How to get time of execution in c++?[Closed]

I tried to implement the basic idea by calling time function together and subtracting them.
But it always gives 0.00 seconds but I don’t think it is possible because I’m testing for plenty of test cases and large inputs :/.
I’m basically trying to analyse the time complexity for fibonacci in all 3 ways.
Here is the code :

There is a “time” command in Linux.
Suppose the name of your executable is a.out and input.txt is file containing all testcases,
then

time ./a.out <input.txt

will give you time of execution for those testcases.

1 Like
I dont see a problem in you code. The time is printed correctly on my local compiler. Ideone doesn't allow you to access their local system's time! 

For knowing the time in windows on your local system you will have to make a file for input and make your code read input from that file.

You can easily do that by-
fstream f("input.txt");

and then you can output to the file just like cout but instead use the fstream variable f
eg.
int x=5;
f<>x;

Using these file operations you can run the code in you local system and know the time.
Note that to use these operations you will need to include fstream header file

Hope this helps! :)
2 Likes

Hey @h1ashdr@gon

you may use this:

 # include < time.h >
int main(void) {
    clock_t tStart = clock();
    /* Do your stuff here */
    printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}

Resource: http://stackoverflow.com/questions/876901/calculating-execution-time-in-c

You can do:

g++ question.cpp
time ./a.out

It will give you the time, for eg:

real 0m0.004s

user 0m0.000s

sys 0m0.004s

But I don’t know which of these we have to consider. If anyone of you get to know about it, then please tell.

Thanks for the info related to linux. No way in windows?

Oh I see,thanks that clears it :slight_smile:

Yes there is! ptime!!
You will need to download “ptime” and need to take care about keeping at some particular location in computer so that you can access that from anywhere. Or keep your .exe and this ptime in same folder and execute ptime a.exe <input.txt