2

I want the exact time to run a program, I use it from clock(). But there is a problem that I can not give an exact time for small n like 2000.

I want it to return the correct answer for n=1000.

#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;

#define show_time(x, y) cout << endl << #x << " : " << y << endl;

int main()
{
    int n;
    cin >> n;
    int *a = new int[n];

    for(int i=0; i<n; i++)
        a[i] = i;
    random_shuffle(a, a+n);
    int last = clock();

    //STL_sort:
    sort(a, a+n);
    int lastP = clock();

    show_time(STL_sort, (double)(lastP-last)/CLOCKS_PER_SEC);
    return 0;
}

The output is 0. (Definitely 0 will not be the answer)

4

1 回答 1

4

你在什么平台上运行?如果您使用的是 Windows,则可以尝试使用高分辨率时间库

如果您可以访问 C++11,则有一个名为的标头chrono具有类似的功能,并且是可移植的(ish)!

于 2013-10-31T13:53:39.273 回答