473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

Jun 20 '06 #1
8 46774

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

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
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

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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ruben Duarte | last post by:
Anyboby knows how I can do a include of a php page without execute the php code that the page have inside. The target is hide the url of the page that I am including and don't permit execute...
3
by: robert | last post by:
Hello, Anybody knows if it's possible to execute python code from an db. db=MySQLdb.connect(host="localhost",user="r",passwd="j",db="v") c=db.cursor() c.execute("""SELECT * FROM table...
10
by: Rusted | last post by:
How can I made time released ASP code? I want to make section of code viewable after a certain date. Something to this effect, but this does not work right: if now() >= "6/24/2005" then...
3
by: Steve | last post by:
I used the QueryPerformanceCounter and QueryPerformanceFrequency methods in my VC++ 6.0 projects to measure correct time spans, for example for timeouts, how much time takes to execute certain...
3
by: Matt | last post by:
Is there a way to execute server code without doing a complete page refresh in .Net? I have an ASP page to convert that uses XMLHTTP and I'm wondering if I should continue to use it in ASPX or do...
9
by: Martijn Mulder | last post by:
Hi group, It is tempting to jump into .NET programming, especially C# in my case. But I have no idea what the realm is of code based on the CLR. Please inform me of the following: -what...
4
by: Michele Petrazzo | last post by:
Hi, I want to execute an external code, that become from a text file (pe), call a function inside it and get its return value: # ext_code.txt def funct2Call(): return True # test.py
0
by: nano2k | last post by:
Hi I develop a client/server application using webservices. So, I have a client app and a webservice that responds to client app requests - nothin' new. In certain circumstances (namely, when...
1
by: anthonypolidori | last post by:
i need to measure time in c# please tell me how to do it how do i print statements i need to create fault s this is console writing
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.