简体   繁体   English

如何使用 chrono 查看我的排序算法每次迭代需要多长时间?

[英]How to use chrono to see how long my sorting algorithm takes each iteration?

I have to run my program a certain amount of times and want to determine the time to takes for each iteration to complete.我必须运行我的程序一定次数,并希望确定每次迭代完成所需的时间。 I changed it to a smaller value to see the results but it shows me我将其更改为较小的值以查看结果,但它显示了我

0.002056
0.001183
0.000613
0.000922
0.000731

I'm a little unsure if this is how the results should look for bubble sort since it begins larger than the end of the results.我有点不确定这是否是结果应该如何查找冒泡排序,因为它开始大于结果的结尾。 Do these results seem accurate or am I calling my timer in a weird position?这些结果看起来准确还是我在一个奇怪的位置调用我的计时器?

if (sorting_type == 1) {
        for (int i = 0; i < 5; i++) {
            auto timer1 = chrono::high_resolution_clock::now();
            A->BubbleSort();
            auto timer2 = chrono::high_resolution_clock::now();
            chrono::duration<double, milli> duration_ms = timer2 - timer1;
            cout << duration_ms.count() << endl;
            
        }  
    }

You are repeatedly sorting the same set of data.您正在重复对同一组数据进行排序。 This leads to Bubble Sort taking way less time after the first iteration.这导致冒泡排序在第一次迭代后花费的时间更少。 If your data looked like this before:如果您的数据以前是这样的:

[6,2,8,1]

These steps would have to happen in the first iteration:这些步骤必须在第一次迭代中发生:

 <->
[2,6,8,1]
   <->
[2,6,8,1]
     <->
[2,6,1,8]
 <->
[2,6,1,8]
   <->
[2,1,6,8]
     <->
[2,1,6,8]
 <->
[1,2,6,8]
   <->
[1,2,6,8]
     <->
[1,2,6,8]
 <->
[1,2,6,8]
   <->
[1,2,6,8]
     <->
[1,2,6,8]

The other iterations would all just look like this:其他迭代都将如下所示:

 <->
[1,2,6,8]
   <->
[1,2,6,8]
     <->
[1,2,6,8]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM