timing of a function

V

Vasileios

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
 
P

Peter Koch Larsen

Vasileios said:
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
 
J

Jonathan Turkanis

Vasileios said:
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
 
C

Chris Theis

Vasileios said:
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
 
J

Joe C

Vasileios said:
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;
}
 
S

Sumit Rajan

Joe C said:
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;
}
 
J

Joe C

Sumit Rajan said:
#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top