【问题标题】:Why is the C++ clock returning different values?为什么 C++ 时钟返回不同的值?
【发布时间】:2017-04-01 17:25:07
【问题描述】:

我想为我的插入排序计时。如果我有这样的代码,它可以工作,但它会为循环中的每种排序打印一次(因为它在循环中):

clock_t start;
double duration;
start = clock();

int j, temp;

for (int i = 0; i < NumberOfLines; i++) {
    j = i;
    while (j > 0 && arr[j - 1] < arr[j]) {
        temp = arr[j];
        arr[j] = arr[j - 1];
        arr[j - 1] = temp;
        j--;
    }
    duration = (clock() - start) / (double)CLOCKS_PER_SEC;
    cout<<"Sorting took: "<< duration<<" seconds"<<'\n';
}
duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
cout << "Please Work: " << duration << " seconds" << '\n';

但如果我在循环内注释cout,它会返回 0 所用时间:

    duration = (clock() - start) / (double)CLOCKS_PER_SEC;
    //cout<<"Sorting took: "<< duration<<" seconds"<<'\n';
}
duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
cout << "Please Work: " << duration << " seconds" << '\n';

为什么?

【问题讨论】:

  • 优化。循环后打印排序数组以防止它。
  • 我敢打赌,因为您的循环的执行时间远远少于捕获时间所需的时间,最重要的是发射到连接到控制台或文件的流。而且通常时钟的粒度并不能很好地捕捉到一个简单循环的几百次迭代。
  • @yurikilochek 是的,这可能是我必须做的。
  • @LaurentG 我不确定,因为如果我在循环中打印时间(上面的第一个代码块)它会打印时间。
  • This question 可能是相关的。

标签: c++ time


【解决方案1】:

今天的 CPU 太快了。

下面的这个排序代码使用Sleep( rand() % 100 )来延迟时间来模拟工作量。

// sort program
// uses Sleep( rand() % 100 ) to delay time to simulate work load.
//

#include <time.h>
#include <iostream>
#include <windows.h>
using namespace std;

int main(  ){

    clock_t start=0, stop=0,start2=0,stop2=0;
    double duration=0,duration2=0, totalDurations=0.0;
    int i=0,j=0, temp=0;
    int NumberOfLines = 10;
    int arr[2001] = { 0 };

    cout<<"using Sleep( rand() % 100 ) to delay time to simulate work load. \n\n";

    // create random seed
    srand(time(0));

    // fill arr[  ] with random numbers
    for (int r = 0; r < NumberOfLines; r++){
        arr[r] = rand() % 1234;
    }

    // start total sort clock
    start = clock();

    //  begin sorting loop
    for ( i = 0; i < NumberOfLines; i++) {

        j = i;

         // start single item sort clock
         start2 = clock();
         Sleep( rand() % 100 );
         //cout << "(" << arr[i] << " ";

        // do actual single item sorting
        while ( (j > 0) && (arr[j - 1] < arr[j]) ) {
            temp = arr[j];
            arr[j] = arr[j - 1];
            arr[j - 1] = temp;
            j--;
        }

         // stop single item sort clock
         stop2 = clock();

         // calculate single item sort   time
         duration2 = (stop2 - start2) / (double)CLOCKS_PER_SEC;
         totalDurations += duration2;

         // display Single item sorting results
         cout << "Single item sorting took: " << duration2 << " seconds" << '\n';
        //cout << " " << arr[i] << ") ,";

    }

    // stop total sort clock
    stop = clock();

    // calculate total   sort   time
    duration = (stop - start) / (double)CLOCKS_PER_SEC;

    // display total  sorting results
    cout << '\n';
    cout << "Total Sorting took: " << duration << " seconds" << " (td.add+= ";
    cout<<" "<< totalDurations <<", error margin +-) \n";

    cout << " \nPress any key to continue\n";
    cin.ignore();
    cin.get();

    return 0;
}

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2014-02-10
    • 2011-06-12
    • 1970-01-01
    • 2013-05-09
    • 2020-08-13
    • 2021-04-19
    • 2014-11-02
    • 1970-01-01
    相关资源
    最近更新 更多