Solved

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

Posted on 2004-03-29
12
3,010 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
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 125 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

23 Experts available now in Live!

Get 1:1 Help Now