C++ 为什么C++;时钟返回不同的值?

C++ 为什么C++;时钟返回不同的值?,c++,time,C++,Time,我想给我的插入排序计时。如果我有这样的代码,它可以工作,但它会为循环中的每个排序打印一次(因为它在循环中): 为什么? 今天的CPU太快了 下面的排序代码使用Sleep(rand()%100)延迟时间来模拟工作负载 // sort program // uses Sleep( rand() % 100 ) to delay time to simulate work load. // #include <time.h> #include <iostream> #incl

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

为什么?

今天的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;
}
//排序程序
//使用睡眠(rand()%100)延迟时间以模拟工作负载。
//
#包括
#包括
#包括
使用名称空间std;
int main(){
时钟开始=0,停止=0,开始=0,停止2=0;
双持续时间=0,持续时间2=0,总持续时间=0.0;
int i=0,j=0,temp=0;
int NumberOfLines=10;
int arr[2001]={0};

coutOptimization。一个接一个地打印排序的数组以防止它。我敢打赌,因为您的循环执行的时间远远少于捕获时间所需的时间,尤其是发送到连接到控制台或文件的流所需的时间。而且通常时钟的粒度并不是很好,无法捕获简单循环的几百次迭代。@yurikilochek是的,这可能是我必须做的。@LaurentG我不确定,因为如果我打印循环中的时间(上面的第一个代码块),它会打印时间。可能相关。
    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';
// 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;
}