You are not logged in. Please login at to post your questions!


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 :

asked 23 Dec '14, 22:31

h1ashdr%40gon's gravatar image

accept rate: 10%

edited 23 Dec '14, 22:43

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
int x=5;
This will output 5 in the file.

To read from file use-

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! :)

answered 23 Dec '14, 22:41

shivam1511's gravatar image

accept rate: 22%

edited 23 Dec '14, 22:52

Oh I see,thanks that clears it :)

(23 Dec '14, 22:43) h1ashdr@gon3★

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.


answered 23 Dec '14, 22:34

bhavesh_munot's gravatar image

accept rate: 0%

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

(23 Dec '14, 22:37) h1ashdr@gon3★

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

(23 Dec '14, 23:20) bhavesh_munot3★

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;



answered 23 Dec '14, 22:46

rishabhprsd7's gravatar image

accept rate: 14%

edited 23 Dec '14, 22:48

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.


answered 28 Dec '14, 15:46

optimus_prime1's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 23 Dec '14, 22:31

question was seen: 3,063 times

last updated: 28 Dec '14, 15:46