By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

Measure time to execute a piece of code

P: n/a
Hi
I want to measure the time required to execute a piece of code.
For example, I want to know the time required to execute a given
function. How do I do it within C++? I know I got to use a library
called ctime. But I dont know what functions to use. Please tell me.

Jun 20 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a

I want to measure the time required to execute a piece of code.
For example, I want to know the time required to execute a given
function. How do I do it within C++? I know I got to use a library
called ctime. But I dont know what functions to use. Please tell me.


include time.h in your C++ file

clock_t start, finish;
start = clock();

// your code here

finish = clock();

( (finish - start)/CLOCKS_PER_SEC )
should give the time taken by your code

~Madhav

Jun 20 '06 #2

P: n/a

MadhavC wrote:
include time.h in your C++ file

clock_t start, finish;
start = clock();

// your code here

finish = clock();

( (finish - start)/CLOCKS_PER_SEC )
should give the time taken by your code

~Madhav


But this gives time in seconds, right? My execution time may be in
milliseconds (or even less). The code you gives 0 time for all
executions. Is there a method to obtain greater resolution?

Jun 20 '06 #3

P: n/a
I reccomend you to use this class:
http://oldmill.uchicago.edu/~wilder/Code/timer/
It is pretty simple and easy to use. I have used it in a lot of my code
snippets, works very well. It is time resolution is also high.

sa********@gmail.com wrote:
Hi
I want to measure the time required to execute a piece of code.
For example, I want to know the time required to execute a given
function. How do I do it within C++? I know I got to use a library
called ctime. But I dont know what functions to use. Please tell me.


Jun 20 '06 #4

P: n/a

But this gives time in seconds, right? My execution time may be in
milliseconds (or even less). The code you gives 0 time for all
executions. Is there a method to obtain greater resolution?


simply don't devide by CLOCKS_PER_SEC
(finish - start) will give you the CPU clocks occured in between.
And I am not sure if sombody can get time detailed to less than the CPU
clock

~Madhav

Jun 20 '06 #5

P: n/a

b.*****@gmail.com wrote:
I reccomend you to use this class:
http://oldmill.uchicago.edu/~wilder/Code/timer/
It is pretty simple and easy to use. I have used it in a lot of my code
snippets, works very well. It is time resolution is also high.


I don't see the resolution of this "class" is below 1 sec.
Am I missing something?

~Madhav

Jun 20 '06 #6

P: n/a
In article <1150802344.936227.233380
@y41g2000cwy.googlegroups.com>, ch*********@gmail.com
says...

[ ... ]
( (finish - start)/CLOCKS_PER_SEC )


Try using:

(double(finish - start)/CLOCKS_PER_SEC)

and you may find the results somewhat more useful.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 20 '06 #7

P: n/a
If you are on a unix flavour, perhaps a util named gprof is useful. It
gives you detailed information, including time spent at a particular
function, number of calls, etc.

I suppose (?) other systems have something similar.

sa********@gmail.com wrote:
Hi
I want to measure the time required to execute a piece of code.
For example, I want to know the time required to execute a given
function. How do I do it within C++? I know I got to use a library
called ctime. But I dont know what functions to use. Please tell me.

Jun 20 '06 #8

P: n/a
It works as the way you explained but it is compact and has a couple of
more features. I didnt used it in a year but I remember it's resolution
is below 1 sec. And I looked at the code and I dont see a reason for
resolution to be 1 sec. Am I missing something too? (Maybe one of us
should try to execute the sample ;) )

MadhavC wrote:
b.*****@gmail.com wrote:
I reccomend you to use this class:
http://oldmill.uchicago.edu/~wilder/Code/timer/
It is pretty simple and easy to use. I have used it in a lot of my code
snippets, works very well. It is time resolution is also high.


I don't see the resolution of this "class" is below 1 sec.
Am I missing something?

~Madhav


Jun 20 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.