calculating execution time

Hi,
I need to know how to calculate execution time of a C++ program. Please help.
check out timers? also code::blocks does it for you automatically i think. im assuming youre running DOS apps though
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.
actually i am trying to find out how much time my code requires to find first 10000000 prime numbers.
And I am new to the programming world. haven't got that far.
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.
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;
Thanks all. Got it.
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.
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.