Thread: count execution time in ubuntu(nanoseconds)

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    44

    count execution time in ubuntu(nanoseconds)

    hi i asked few days back if it was possible to count the execution time using clock, well i got replies telling that its not possible

    my code runs on linux, are there any specific functions that can be used only in linux in order count the execution time in nanoseconds? and then get the seconds?

    for example if we have a running time in nanoseconds

    33939

    i want 33939*10^-9 = 0.000033939 seconds

    i ve researched a little bit and i found that the clock_gettime can do the job

    but i havent found any source code on how to do this if you could help me with a simple sample code or suggest me something else it would be appreciated


    i have found another one for gettimeofday

    Code:
    #include <iostream>
    #include <sys/time.h>
    using namespace std;
    
    int main(void)
    {
    timeval tim;
                 gettimeofday(&tim, NULL);
                 double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
    
                        int i,j=0;
                        for(i=0;i<1000;i++){
                                j=j+1;
                       }
                 gettimeofday(&tim, NULL);
                 double t2=tim.tv_sec+(tim.tv_usec/1000000.0);
                 cout<<t2-t1<<" seconds elapsed\n";
    
    
    }
    gives output

    5.00679e-06 seconds elapsed


    is this correct?? but it is not in nanoseconds
    Last edited by nik; 04-06-2011 at 09:44 AM.

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by nik View Post
    i want 33939*10^-9 = 0.000033939 seconds
    Quote Originally Posted by nik View Post
    5.00679e-06 seconds elapsed
    is this correct?? but it is not in nanoseconds
    I'm confused. Do you want the answer in seconds or nanoseconds? If it's in seconds and you want nanoseconds, just multiply by 1,000,000,000. I don't see what there is to be confused about.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466

    Smile

    If you're worried about execution time down to nanoseconds, you've got bigger problems!

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I am guessing that timeval.tv_usec actually gives usec known as microseconds.
    So to go to nanoseconds is impossible.

    As a note, you shouldn't divide like that. You should save into two separate timevals, subtract the tv_sec and tv_usec respectively. Not of carriers on the tv_usec, so you should make a function to deal with this (probably already available).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Partition and scheduling HW/SW help!
    By thangdc01_02 in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2010, 02:07 PM
  2. Grouping Question
    By baikal_m in forum C Programming
    Replies: 7
    Last Post: 10-26-2010, 04:41 PM
  3. mandelbrot set program improvements
    By mad_muppet in forum Game Programming
    Replies: 3
    Last Post: 07-14-2010, 05:58 AM
  4. Mesure time elapsed for each statement execution
    By reg13 in forum C Programming
    Replies: 2
    Last Post: 08-20-2009, 11:04 PM
  5. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM