Thread: Calculate time in microseconds or nanoseconds

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    3

    Calculate time in microseconds or nanoseconds

    Hello

    In Java the following loop calculates me the results in nanoseconds and with the addition of /1000 therefore in microseconds.
    Here the incomplete programm. The important part is the loop of 39 and two values which will be printed out.

    Code:
    for(int sx=1; sx<=39; sx++){
                 long startTime = java.lang.System.nanoTime();       
                 System.out.println("Hello world");
                 long endTime = java.lang.System.nanoTime();
                 System.out.println((endTime - startTime)/1000); 
             }

    I would like to do the same in C:
    There are some examples in the internet unfortunately it returns 0 microseconds.

    This is the code I have found in the internet:

    Code:
    /**
     * Get time difference in microseconds
     * */
      
    #include <stdio.h>
    #include <sys/time.h>
      
    double time_diff(struct timeval x , struct timeval y);
      
    int main()
    {  
        int i;
         
        struct timeval before , after;
        gettimeofday(&before , NULL);
       
        //Time taking task
        for (i=1 ; i <= 100 ; i++)
        {
            printf("%d %d %d n",i, i*i, i*i*i);
        }
       
        gettimeofday(&after , NULL);
     
        printf("Total time elapsed : %.0lf us" , time_diff(before , after) ); 
       
        return 0;
    }
     
    double time_diff(struct timeval x , struct timeval y)
    {
        double x_ms , y_ms , diff;
         
        x_ms = (double)x.tv_sec*1000000 + (double)x.tv_usec;
        y_ms = (double)y.tv_sec*1000000 + (double)y.tv_usec;
         
        diff = (double)y_ms - (double)x_ms;
         
        return diff;
    }
    I have changed this code to:

    Code:
    /**
     * Get time difference in microseconds
     * */
    
    #include <stdio.h>
    #include <sys/time.h>
    
    double time_diff(struct timeval x , struct timeval y);
    
    int main()
    {
        int i;
    
        struct timeval before , after;
    
    
        //Time taking task
        for (i=1 ; i <= 39 ; i++)
        {
            gettimeofday(&before , NULL);
            printf("Hello world\n");
            gettimeofday(&after , NULL);
            printf("Total time elapsed : %.0lf us\n" , time_diff(before , after) );
        }
    
        return 0;
    }
    
    double time_diff(struct timeval x , struct timeval y)
    {
        double x_ms , y_ms , diff;
    
        x_ms = (double)x.tv_sec + (double)x.tv_usec;
        y_ms = (double)y.tv_sec + (double)y.tv_usec;
    
        diff = (double)y_ms - (double)x_ms;
    
        return diff;
    }


    This is another example:
    Again 0 value.

    Code:
    #include <sys/time.h>
    #include <stdio.h>
    #include <unistd.h>
    int main()
    {
        struct timeval start, end;
    
        long mtime, seconds, useconds;
    
    
        for(int sx=1; sx<=39; sx++){
    
            gettimeofday(&start, NULL);
            printf("Hello world");
            gettimeofday(&end, NULL);
    
            printf("Elapsed time: %ld microsecons\n", end.tv_usec - start.tv_usec);
    
         }
    
        return 0;
    }
    Any idea?

    Thx,
    Troix

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    clock_gettime(3): clock/time functions - Linux man page

    Don't confuse precision with accuracy.

    The internal clock is perfectly within spec if it simply adds 1000 to the nSec value once every uSec.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    3
    Hello

    Would you please demonstrate your idea on one of the examples?
    I was a bit confused about the latency of my post, sorry for the second one.
    Last edited by Troix; 06-05-2017 at 06:36 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Based on how you managed to figure out how to use gettimeofday, I'd say you're more than capable of at least giving it a go yourself first.

    You can always post here if you get really stuck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makes Time() to return the time in microseconds
    By kirill578 in forum C Programming
    Replies: 9
    Last Post: 06-03-2011, 01:04 AM
  2. count execution time in ubuntu(nanoseconds)
    By nik in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2011, 07:20 PM
  3. Calculate time
    By Goo in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2009, 05:24 PM
  4. Determine process time in microseconds
    By kenkoh in forum C Programming
    Replies: 9
    Last Post: 02-18-2008, 02:16 PM
  5. Execution Time in Microseconds?
    By thetinman in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2007, 01:32 PM

Tags for this Thread