C++, Linux (gcc) - equivalent of hrtime for calculating execution time

I need to calculate the execution time of a C++ code segment. On solaris I could do this by using hrtime (high resolution timer) and by starting and stopping the timer before and after the particular section of code. How can I do this on Linux? Is there an equivalent of hrtime in Linux? I need nanosecond precision.
sajith_pkAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
da99rmdConnect With a Mentor Commented:
I havent tested this my self got it when i was making a school project but i think it works.
t can be usfull when working with ticks to get it into "real" time.

long ticks2nano(unsigned int tick)
{
long tick_sek, tick_nano;
tick_sek = sysconf(_SC_CLK_TCK);
tick_nano = tick_sek/100000;
return tick_nano*(long)tick;
}

/Rob
0
 
Karl Heinz KremerCommented:
Not yet... You can get a patch (pre-alpha, so this may not work correctly) here: http://high-res-timers.sourceforge.net/

0
 
sunnycoderCommented:
Hi sajith_pk,

I faced exactly the same problem and I ended up using assembly .... Here is the link to that question

http:Q_20762690.html
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
stefan73Commented:
Hi sajith_pk,
The most finely granulated counter are the CPU clock cycles, as measured by the RDTSC ASM instruction:

#include <stdio.h>

typedef unsigned long long int cycle_t__;

__inline__ static cycle_t__ rdtsc__()
   {
     cycle_t__ x;
     __asm__ volatile ("rdtsc" : "=A" (x));
     return x;
   }


   
int main(){
      cycle_t__ xx;

      xx=rdtsc__();
      do_something_lengthy();
      xx = rdtsc__() - xx;
      printf("The time in CPU cycles is: %llu\n",xx);
}

...you just need to measure beforehand what frequency your CPU quartz is running on (like rdtsc; sleep(1); rdtsc).

Cheers,
Stefan
0
 
sajith_pkAuthor Commented:
Thanks sunnycoder and stefan73. I'm trying the ASM option which you guys have suggested. I was trying to find the CPI Clock freq and I got stuck with the sleep on Linux. My compiler (g++) says it cannot find it. Which header file does it beong to, any idea?

And a basic question, once I get the CPU Freq how will I use that along with the value returned by rdtsc() to calculate the actual time.

-Sajith
0
 
sunnycoderCommented:
#include <unistd.h>

the idea that stfan was trying to get across was

rdtsc will give you number of CPU cycles ... get the counter value before and after sleep(1) and you get number of cycles per second ...

number of cycles for your code to execute / number of cycles per second
will give you the number of seconds for which the code executed

the only problem is that sleep will not return you control after exactly 1 second ... there may be delays due to scheduling and other factors

it will be better to get the information from proc filesystem if available ( I think /proc/cpuinfo should have it)

If your purpose is served by knowing the number of CPU cycles elapsed, it will be quite accurate
0
 
da99rmdCommented:
Or just take a look at my post 4 posts up :)

/Rob
0
 
stefan73Commented:
sunnycoder,
> not return you control after exactly 1 second

True, but normally precision you get by this should be sufficient (perhaps like +/- 0.5%).

The important thing if you want to compare runs is that you use the same CPU speed measurement.

Stefan
0
 
stefan73Commented:
da99rmd,
> sysconf
That looks interesting. I'll check.

Stefan
0
 
stefan73Commented:
Be aware that RDTSC might screw up results on multi-CPU systems. Although their timers *should* be in sync, don't rely on that.
0
 
sajith_pkAuthor Commented:
Ohhh! Then I have a problem :)..I'm doing this on a 4 CPU machine.  

Rob, what is that argument for the function ticks2nano?

-Sajith
0
 
da99rmdCommented:
The number of clock ticks elapsed during the code execution.
You have to change the in to something longer if you are going to work with all 64 bits of the counter.

/Rob
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.