I am trying to figure out time difference between iterative and recursive function but I am not able to even find out difference between for loop. Anyone has any idea about this ??
int main (){
//int arr[7] = {1,2,3,4,5,6,7};
ll arr[100000];
unsigned __int64 one = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
for (ll i = 0; i < 100000; i )
{
arr[i]=i 1;
}
unsigned __int64 two = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
// int a=binSearchRecursive(arr,6755,0,10000);
// int b=binSearchIter(arr,6755,0,10000);
// unsigned __int64 three = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
cout << "Recursive :"<< one << " " <<two <<endl;
cout << "Iterative :" <<two-one << std :: endl;
// cout <<a<<" " << b ;
return 0;
}
Output -
Recursive :1635504404011 1635504404011
Recursive :0
I just added 2nd loop to replace existing values
int main (){
//int arr[7] = {1,2,3,4,5,6,7};
ll arr[100000];
unsigned __int64 one = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
for (ll i = 0; i < 100000; i )
{
arr[i]=i*3;
}
for (long long i = 0; i < 100000; i )
{
arr[i]=arr[i]*arr[i 1];
}
unsigned __int64 two = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
// int a=binSearchRecursive(arr,6755,0,10000);
// int b=binSearchIter(arr,6755,0,10000);
// unsigned __int64 three = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
//cout << "Recursive :"<< one << " " <<two <<endl;
cout << "Recursive :"<< two-one <<endl;
//int b = arr[23423] arr[73432];
//cout << "Iterative :" << three- two << std :: endl;
// cout <<a<<" " << b ;
return 0;
}
Now I am getting output as -
Recursive :1029000
So which difference is this ?? Is it for both loops or just for 1st ??
CodePudding user response:
What is happening is that the compiler is optimizing the loop away. Usually you'd disable optimizations, but if you don't want to outright disable it, you can add the volatile
keyword to your variable arr
and you will notice a huge speed difference:
#include <iostream>
#include <chrono>
int main() {
using namespace std;
volatile long long arr[100000];
auto one = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
for (long long i = 0; i < 100000; i )
{
arr[i] = i * 3;
}
for (long long i = 0; i < 100000; i )
{
arr[i] = arr[i] * arr[i 1];
}
auto two = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
cout << "Recursive :" << two - one << endl;
return 0;
}
My result on MSVC 2019 with all optimizations enabled:
Recursive :88800
The volatile
keyword tells the compiler not to optimize a variable, in any way.
CodePudding user response:
it's optimized.
#include <iostream>
#include <sstream>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <fstream>
#include <unistd.h>
#include <filesystem>
#include <iostream>
#include <fstream>
#include <map>
#include <cmath>
#include <chrono>
#include <algorithm>
#include <vector>
#include <execution>
#include <thread>
#include <condition_variable>
#include <mutex>
#include <string>
#include <atomic>
int main(int argc, char *argv[])
{
long long arr[100000];
auto t1 = std::chrono::system_clock::now();
for (long long i = 0; i < 100000; i )
{
arr[i]=i*3;
}
auto t2 = std::chrono::system_clock::now();
std::chrono::nanoseconds f = std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1);
std::cout << f.count() << std::endl;
return 0;
}
c -std=c 17 -O3 'first 3.cpp' -ltbb -lpthread -o t