Domanda

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)

È stato utile?

Soluzione

What platform are you running on? If you're on Windows, you could try the high-resolution time library.

If you have access to C++11, there is a header called chrono that has similar functionality, and is portable (ish)!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top