为什么我没有得到任何时间差异之前和之后的循环在CPP?



我试图找出迭代和递归函数之间的时差,但我甚至无法找出for循环之间的差异。有人对此有什么想法吗?

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;
}

输出——

Recursive :1635504404011 1635504404011
Recursive :0

我刚刚添加了第二个循环来替换现有的值

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;
}

现在我得到的输出是-

Recursive :1029000

那么这是什么区别??它是两个循环还是只是第一个循环?

编译器正在优化循环。通常你会禁用优化,但如果你不想完全禁用它,你可以添加volatile关键字到你的变量arr,你会注意到一个巨大的速度差异:

#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;
}

我在启用所有优化的MSVC 2019上的结果:

Recursive :88800

volatile关键字告诉编译器不要以任何方式优化变量。

已优化。

#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

相关内容

  • 没有找到相关文章

最新更新