Connecting Tech Pros Worldwide Help | Site Map

Measure time to execute a piece of code

saurabh.ss@gmail.com
Guest
 
Posts: n/a
#1: Jun 20 '06
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.

MadhavC
Guest
 
Posts: n/a
#2: Jun 20 '06

re: Measure time to execute a piece of code



[color=blue]
> 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.[/color]

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

saurabh.ss@gmail.com
Guest
 
Posts: n/a
#3: Jun 20 '06

re: Measure time to execute a piece of code



MadhavC wrote:[color=blue]
> 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[/color]

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?

b.evrim@gmail.com
Guest
 
Posts: n/a
#4: Jun 20 '06

re: Measure time to execute a piece of code


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.

saurabh.ss@gmail.com wrote:[color=blue]
> 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.[/color]

MadhavC
Guest
 
Posts: n/a
#5: Jun 20 '06

re: Measure time to execute a piece of code


[color=blue]
>
> 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?[/color]

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

MadhavC
Guest
 
Posts: n/a
#6: Jun 20 '06

re: Measure time to execute a piece of code



b.evrim@gmail.com wrote:[color=blue]
> 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.
>[/color]

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

~Madhav

Jerry Coffin
Guest
 
Posts: n/a
#7: Jun 20 '06

re: Measure time to execute a piece of code


In article <1150802344.936227.233380
@y41g2000cwy.googlegroups.com>, choudharymv@gmail.com
says...

[ ... ]
[color=blue]
> ( (finish - start)/CLOCKS_PER_SEC )[/color]

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.
Gerard Kramer
Guest
 
Posts: n/a
#8: Jun 20 '06

re: Measure time to execute a piece of code


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.

saurabh.ss@gmail.com wrote:[color=blue]
> 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.
>[/color]
bitkidoku
Guest
 
Posts: n/a
#9: Jun 20 '06

re: Measure time to execute a piece of code


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:[color=blue]
> b.evrim@gmail.com wrote:[color=green]
> > 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.
> >[/color]
>
> I don't see the resolution of this "class" is below 1 sec.
> Am I missing something?
>
> ~Madhav[/color]

Closed Thread