calculating execution time

Apr 11, 2011 at 9:05pm
Hi,
I need to know how to calculate execution time of a C++ program. Please help.
Apr 11, 2011 at 9:17pm
check out timers? also code::blocks does it for you automatically i think. im assuming youre running DOS apps though
Apr 11, 2011 at 9:21pm
The best thing to do I would think is to assign a constructor and a destructor to the application itself that drops a time stamp into a file. This way you get the time stamp after the program is loaded but before the execution of the entry point, and right when the last objects are being destroyed. This should be accurate enough for most people.
Apr 11, 2011 at 9:22pm
actually i am trying to find out how much time my code requires to find first 10000000 prime numbers.
Apr 11, 2011 at 9:24pm
And I am new to the programming world. haven't got that far.
Apr 11, 2011 at 9:28pm
That sounds incredibly.... boring. But hey if it's what you're into then go for it I guess. What IDE are you using? Also, if you know, what Compiler? This is relavent to defining the functions I mentioned earlier. If you don't know what Compiler I'll assume the default setting for that IDE.
Apr 11, 2011 at 9:36pm
closed account (D80DSL3A)
Try this to get the execution time in milliseconds for some block of code:
#include <ctime>// include this header

in main()
1
2
3
4
int start_s=clock();
	// the code you wish to time goes here
int stop_s=clock();
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;
Apr 11, 2011 at 9:47pm
Thanks all. Got it.
Apr 11, 2011 at 9:53pm
That will work for a simple project but remember that your first time stamp doesn't execute until "start_s" is initialized in fun2code's example. The same can be said about the final time stamp and "stop_s".

Also, the "clock()" function doesn't return milliseconds. The value is dependent on the speed of the host systems processor. But this will give you a relative score.
Apr 11, 2011 at 10:08pm
its also going to take longer depending on how you coded it. if youre going through every number up to 10000000 and counting its factors, its gonna take a damn long time
Topic archived. No new replies allowed.