You are not logged in. Please login at www.codechef.com 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 : http://ideone.com/iRiXrj

asked 23 Dec '14, 22:31

h1ashdr%40gon's gravatar image

3★h1ashdr@gon
2912319
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
eg.
int x=5;
f<<x;
This will output 5 in the file.

To read from file use-
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! :)
link

answered 23 Dec '14, 22:41

shivam1511's gravatar image

5★shivam1511
4382414
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.

link

answered 23 Dec '14, 22:34

bhavesh_munot's gravatar image

3★bhavesh_munot
1981513
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;
}

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

link

answered 23 Dec '14, 22:46

rishabhprsd7's gravatar image

2★rishabhprsd7
1.9k11242
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.

link

answered 28 Dec '14, 15:46

optimus_prime1's gravatar image

2★optimus_prime1
36139
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×1,901
×196
×62
×15

question asked: 23 Dec '14, 22:31

question was seen: 3,063 times

last updated: 28 Dec '14, 15:46