Solved

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

Posted on 2004-03-29
12
3,001 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:sajith_pk
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 44

Expert Comment

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

0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
 
LVL 8

Accepted Solution

by:
da99rmd earned 125 total points
Comment Utility
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
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
 

Author Comment

by:sajith_pk
Comment Utility
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
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
#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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 8

Expert Comment

by:da99rmd
Comment Utility
Or just take a look at my post 4 posts up :)

/Rob
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
da99rmd,
> sysconf
That looks interesting. I'll check.

Stefan
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
 

Author Comment

by:sajith_pk
Comment Utility
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
 
LVL 8

Expert Comment

by:da99rmd
Comment Utility
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now