473,383 Members | 1,801 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,383 software developers and data experts.

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 5077

"Vasileios" <va*******@zografos.org> wrote in message
news:40**************************@posting.google.c om...
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*******@zografos.org> skrev i en meddelelse
news:40**************************@posting.google.c om...
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*******@zografos.org> wrote in message
news:40**************************@posting.google.c om...
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*******@zografos.org> wrote in message
news:40**************************@posting.google.c om...
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<iostream>

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<float>(t_off - t_on))/CLOCKS_PER_SEC
<< " seconds" << endl;

return 0;
}
Jul 22 '05 #5

"Joe C" <jk*****@bellsouth.net> wrote in message
news:U7******************@bignews3.bellsouth.net.. .

"Vasileios" <va*******@zografos.org> wrote in message
news:40**************************@posting.google.c om...
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<iostream>

#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<float>(t_off - t_on))/CLOCKS_PER_SEC
<< " seconds" << endl;

return 0;
}

Jul 22 '05 #6

"Sumit Rajan" <su********@myrealbox.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
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...
7
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...
7
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...
7
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. ...
2
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...
15
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...
2
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"...
5
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...
2
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.