473,804 Members | 3,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

timing of a function

Hi everyone.

I am trying to time how long a function takes to execute in c++.

Lets say I have a loop for example:

for (int i=0;i<1000; i++)
{

....do something useful here
}
I would like to measure how long this loop takes to execute in
miliseconds or seconds.

How can I do that?

I thought of using time() to get the current time before the loop, and
once again after the loop has finished.

However this doesnt seem very robust. Can you perhaps recommend
another way?

Thank you in advance
Vasileios Zografos
Jul 22 '05 #1
6 5109

"Vasileios" <va*******@zogr afos.org> wrote in message
news:40******** *************** ***@posting.goo gle.com...
Hi everyone.

I am trying to time how long a function takes to execute in c++.

Lets say I have a loop for example:

for (int i=0;i<1000; i++)
{

...do something useful here
}
I would like to measure how long this loop takes to execute in
miliseconds or seconds.

How can I do that?


{
boost::progress _timer t;
for (int i=0;i<1000; i++)
{
// ...do something useful here
}
}

This will print the elapsed time to the console. boost::timer offers
more functionality, such as the ability to query the elapsed time.
See http://www.boost.org/libs/timer/timer.htm.

Jonathan
Jul 22 '05 #2

"Vasileios" <va*******@zogr afos.org> skrev i en meddelelse
news:40******** *************** ***@posting.goo gle.com...
Hi everyone.

I am trying to time how long a function takes to execute in c++.

Lets say I have a loop for example:

for (int i=0;i<1000; i++)
{

...do something useful here
}
I would like to measure how long this loop takes to execute in
miliseconds or seconds.

How can I do that?

I thought of using time() to get the current time before the loop, and
once again after the loop has finished.

However this doesnt seem very robust. Can you perhaps recommend
another way?

Thank you in advance
Vasileios Zografos


You must resort resort to non-standard C++ programming here. You can look up
what functions are available on your operating system. For an intel system,
I can recommend you to google for the RDTSC assembly instruction.

Kind regards
Peter
Jul 22 '05 #3

"Vasileios" <va*******@zogr afos.org> wrote in message
news:40******** *************** ***@posting.goo gle.com...
Hi everyone.

I am trying to time how long a function takes to execute in c++.

Lets say I have a loop for example:

for (int i=0;i<1000; i++)
{

...do something useful here
}
I would like to measure how long this loop takes to execute in
miliseconds or seconds.

How can I do that?

I thought of using time() to get the current time before the loop, and
once again after the loop has finished.

However this doesnt seem very robust. Can you perhaps recommend
another way?


For (more or less) exact measurements I'd personally recommend the use of
profilers. However, you can certainly resort to the use of the time()
function but in order to obtain representative measurements the timing must
be repeated a couple of times. To do this you could use the following macro:

////////////////////////////////////////////////////////////////////////////
//
// BENCH
// benchmark for function timing.
// e.g.:
//
// double t;
// BENCH( sort( Vec.begin(), Vec.end() ), "sort of Vec", 2.0, t );
////////////////////////////////////////////////////////////////////////////
//
#define BENCH( Block, Name, TimeOut, t) \
{ \
time_t ts, te; \
double total_t; \
int ni = 1; \
do { \
ts = time((time_t *)NULL); \
for( int i = 0; i < ni; i++) { \
Block; \
} \
te = time((time_t *)NULL); \
t = (total_t=(te - ts)) / ni; \
ni <<= 1; \
} while( total_t < TimeOut); \
std::cout << "time for one " << Name << " = " << t << " secs" <<
std::endl; \
} \

HTH
Chris
Jul 22 '05 #4

"Vasileios" <va*******@zogr afos.org> wrote in message
news:40******** *************** ***@posting.goo gle.com...
Hi everyone.

I am trying to time how long a function takes to execute in c++.

Lets say I have a loop for example:

for (int i=0;i<1000; i++)
{

...do something useful here
}
I would like to measure how long this loop takes to execute in
miliseconds or seconds.

How can I do that?

I thought of using time() to get the current time before the loop, and
once again after the loop has finished.

However this doesnt seem very robust. Can you perhaps recommend
another way?

Thank you in advance
Vasileios Zografos


This is probabally no help, but you can use the clock() function along with
the constant CLOCKS_PER_SEC to for a quick and dirty timer. Note that
clock() records process time, not real time...but as a practical matter, it
will give you the info you desire. Also note, if you are just including the
timer for yourself, you can just report the (clock() - t_on) without
casting. However the units will depend on the system/compiler...

#include<iostre am>

using namespace std;

void func(const int& numin){
int mytimer = clock();
for(char i = 0; i < numin; ++i){
for (int j = 0; j < numin; ++j){
cout << i << j << ' ';
}
}
}

int main(){
int anInt = 100;

int t_on = clock(); // timer before calling func
func(anInt);
int t_off = clock(); // timer when func returns

cout << "Function completed in "
<< (static_cast<fl oat>(t_off - t_on))/CLOCKS_PER_SEC
<< " seconds" << endl;

return 0;
}
Jul 22 '05 #5

"Joe C" <jk*****@bellso uth.net> wrote in message
news:U7******** **********@bign ews3.bellsouth. net...

"Vasileios" <va*******@zogr afos.org> wrote in message
news:40******** *************** ***@posting.goo gle.com...
Hi everyone.

I am trying to time how long a function takes to execute in c++.

Lets say I have a loop for example:

for (int i=0;i<1000; i++)
{

...do something useful here
}
I would like to measure how long this loop takes to execute in
miliseconds or seconds.

How can I do that?

I thought of using time() to get the current time before the loop, and
once again after the loop has finished.

However this doesnt seem very robust. Can you perhaps recommend
another way?

Thank you in advance
Vasileios Zografos
This is probabally no help, but you can use the clock() function along

with the constant CLOCKS_PER_SEC to for a quick and dirty timer. Note that
clock() records process time, not real time...but as a practical matter, it will give you the info you desire. Also note, if you are just including the timer for yourself, you can just report the (clock() - t_on) without
casting. However the units will depend on the system/compiler...

#include<iostre am>

#include <ctime>

using namespace std;

void func(const int& numin){
int mytimer = clock();
for(char i = 0; i < numin; ++i){
for (int j = 0; j < numin; ++j){
cout << i << j << ' ';
}
}
}

int main(){
int anInt = 100;

int t_on = clock(); // timer before calling func
func(anInt);
int t_off = clock(); // timer when func returns

cout << "Function completed in "
<< (static_cast<fl oat>(t_off - t_on))/CLOCKS_PER_SEC
<< " seconds" << endl;

return 0;
}

Jul 22 '05 #6

"Sumit Rajan" <su********@myr ealbox.com> wrote in message
news:bu******** ****@ID-206022.news.uni-berlin.de...

#include <ctime>


Thanks...and sorry for the omission. My IDE/compiler (dev-c++) appears to
#include a bunch of stuff that I don't explicitly ask for...which makes me
somewhat ignorant of the proper headers that are required...

Also note...the first line in func is not used.

Joe
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8134
by: Russell | last post by:
I have a quirky issue that I believe involves timing and only 2 hairs left to pull. I have a modal dialog that is an IFrame. The IFrame contains another window - which contains the appropriate title. I am trying to change the title of the IFrame window to be that of the contained window title. If I uncomment the alert statement below - the title change works. Comment out the alert - and - no title change.
7
2770
by: Droopy | last post by:
Hi, I don't understand why sometimes, a loop running in a thread takes much more time than usual. I have a thread that must call each 20 ms a C++ unmanaged function. I made a C++ managed wrapper for calling this function from C# (see below _serialChannel.ItTask ()). It works great but for this timing problem. Here below, the main loop for this thread :
7
3771
by: jamie | last post by:
hey all, I am attempting to do motion control for a final project, but I have a concern.... For motion control, timing is everyting, the better it is, the better it works. Currently I am trying to use the timer function block to do a 10ms loop, but if I remember correctly.... that timing
7
1170
by: Steven D'Aprano | last post by:
I have two code snippets to time a function object being executed. I expected that they should give roughly the same result, but one is more than an order of magnitude slower than the other. Here are the snippets: def timer1(): timer = time.time func = lambda : None itr = * 1000000
2
3309
by: Steven D'Aprano | last post by:
The timeit module is ideal for measuring small code snippets; I want to measure large function objects. Because the timeit module takes the code snippet argument as a string, it is quite handy to use from the command line, but it is less convenient for timing large pieces of code or when working in the interactive interpreter. E.g. variations on this *don't* work: $ python Python 2.4.3 (#1, Jun 13 2006, 11:46:08)
15
2586
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to determine who needs to receive the text message then send the message to the address. Only problem is, the employee may receive up to 4 of the same messages because each thread gets the recors then sends the message. I need somehow to prevent...
2
1212
by: kevin | last post by:
Hi... I'm having a few problems here... I want to input in my function different values and then time each one to see how long it took to run the function... so right now it doesn't like the "i" inside my function fibonacci(i) but if I put a constant it's fine.... any idea on how to approach this? thanks! Jonathan The Code:
5
1750
by: =?Utf-8?B?dWxpbGxpbGxpYQ==?= | last post by:
I've had two issues plaguing me for 4 months now and I haven't gotten anywhere. I'm into making 2D games and these things are essential to games. These are my issues: 1. I need timing precise to about 4 milliseconds at most. Currently, I've been using the clock() function, but it's only precise to 1/64 second (or about 15.6 milliseconds). For those running their monitor at 60 Hz, this isn't much of a problem, but for those running...
2
1549
by: julie.siebel | last post by:
Google apparently ate my original post to this (grr) so this'll be a bit more vague than the initial post, but...*sigh*. Javascript is not my forte, and I apologize for the acky-ness of the code. I'm working on a page for a travel company. There's way too much going on with it/on it, but I'm kind of stuck at this point - the client loves it, so I have to solve this problem.
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.