?
Solved

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

Posted on 2004-03-29
12
Medium Priority
?
3,044 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10709428
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
ID: 10710575
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 375 total points
ID: 10712755
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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
LVL 12

Expert Comment

by:stefan73
ID: 10713400
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
ID: 10714861
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
ID: 10721304
#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
 
LVL 8

Expert Comment

by:da99rmd
ID: 10721652
Or just take a look at my post 4 posts up :)

/Rob
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10721775
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
ID: 10721790
da99rmd,
> sysconf
That looks interesting. I'll check.

Stefan
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10722071
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
ID: 10744973
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
ID: 10755426
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 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