Connecting Tech Pros Worldwide Help | Site Map

Measure time to execute a piece of code

 
LinkBack Thread Tools Search this Thread
  #1  
Old June 20th, 2006, 10:45 AM
saurabh.ss@gmail.com
Guest
 
Posts: n/a
Default Measure time to execute a piece of code

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.


  #2  
Old June 20th, 2006, 11:15 AM
MadhavC
Guest
 
Posts: n/a
Default 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

  #3  
Old June 20th, 2006, 11:55 AM
saurabh.ss@gmail.com
Guest
 
Posts: n/a
Default 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?

  #4  
Old June 20th, 2006, 12:25 PM
b.evrim@gmail.com
Guest
 
Posts: n/a
Default 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]

  #5  
Old June 20th, 2006, 12:35 PM
MadhavC
Guest
 
Posts: n/a
Default 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

  #6  
Old June 20th, 2006, 12:35 PM
MadhavC
Guest
 
Posts: n/a
Default 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

  #7  
Old June 20th, 2006, 01:05 PM
Jerry Coffin
Guest
 
Posts: n/a
Default 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.
  #8  
Old June 20th, 2006, 01:35 PM
Gerard Kramer
Guest
 
Posts: n/a
Default 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]
  #9  
Old June 20th, 2006, 02:05 PM
bitkidoku
Guest
 
Posts: n/a
Default 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]

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.