Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 计算排序算法对整数数组排序所用的时间(毫秒)_C++_Time - Fatal编程技术网

C++ 计算排序算法对整数数组排序所用的时间(毫秒)

C++ 计算排序算法对整数数组排序所用的时间(毫秒),c++,time,C++,Time,我试图比较两种排序算法merge-sort和quick-sort,看哪一种排序像{8,7,6,5,4,3,2,1}这样的小整数数组花费的时间最少 我知道我不应该使用clock(),我一直在尝试time(),但每次它总是给我0表示所用的时间。我也尝试过使用Chrono,它也只给了我0 有人能帮我解决这个问题吗 排序已经完成,工作正常,我就是这样设置的 int Quiarr[] = {8, 7, 6, 5, 4, 3, 2, 1}; int arr_size = sizeof(Quiarr)/siz

我试图比较两种排序算法merge-sort和quick-sort,看哪一种排序像{8,7,6,5,4,3,2,1}这样的小整数数组花费的时间最少 我知道我不应该使用clock(),我一直在尝试time(),但每次它总是给我0表示所用的时间。我也尝试过使用Chrono,它也只给了我0 有人能帮我解决这个问题吗

排序已经完成,工作正常,我就是这样设置的

int Quiarr[] = {8, 7, 6, 5, 4, 3, 2, 1};
int arr_size = sizeof(Quiarr)/sizeof(Quiarr[0]);
start = time();
mergeSort(Quiarr, 0, arr_size - 1);
end = time();
msecs = ((double) (end - start)) * 1000 / CLOCKS_PER_SEC;
cout << msecss2 << " " << endl;
intquiarr[]={8,7,6,5,4,3,2,1};
int arr_size=sizeof(Quiarr)/sizeof(Quiarr[0]);
开始=时间();
合并排序(Quiarr,0,arr_size-1);
结束=时间();
毫秒=((双精度)(结束-开始))*1000/时钟/秒;
试试这个

auto t0 = std::chrono::high_resolution_clock::now();
// your sorting function here
auto t1 = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1);
auto t0=std::chrono::high_resolution_clock::now();
//你的分类功能在这里
自动t1=std::chrono::高分辨率时钟::现在();
自动运行时间=标准时间:持续时间(t2-t1);
高分辨率时钟::now(),具有精确的粒度


<强>注释:需要运行函数的多个试验,这是因为OS调度程序,经过的时间不是常数,并且需要考虑最小的值。(运行一个循环,重复上面的代码1000次,然后打印经过的时间重复次数最多的最小值更合理)。

您能告诉我们到目前为止您得到了什么吗?好的计时器:排序如此小的数组可能需要的时间太短,计时器无法测量。您可以尝试对八个元素进行排序,最多需要几微秒<代码>时间
通常测量秒数。即使在循环1000次之后,所有来自代码的输出也将持续到0。然后尝试以下操作:自动运行=t2-t1;标准::cout