Thread: Determine process time in microseconds

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Determine process time in microseconds

    Hi there,

    Can I ask what is the simplest method to determine the time it takes to execute specific process (within a program) with microsecond precision?

    I've looked at the "time_val" command but couldn't really understand how it works. An example is given below. Can anyone please advise me?

    Code:
    #include <stdio.h>
    int main()
    {
        int data[2][3] =
        {
           1, 2, 3,
           4, 5, 6,
        };
    
        size_t obj_size = sizeof(int);
        size_t obj_cnt = sizeof(data)/sizeof(int);
        char *filename = "output.dat";
        FILE *p_file;
    
        p_file = fopen(filename, "w");
    
        // Clock start time (t1)
        fwrite(&data, obj_size, obj_cnt, p_file);
        //Clock end time (t2)
    
    // Do a subtraction (t2-t1)
    
        fclose(p_file);
     
        return 0;
    }
    Thanks for any help in advance.

    Ken

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I don't know how you could achieve that, but would it not be possible for you to run a process 1000 times in a row then measure it in millisecs?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It might be difficult with such high precision. Still, there may be a few high performance timers out there. I don't remember their names for Windows, however...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    High performance timers? Anything for Linux please!

    I've looked at some of the examples running "for" loops for specific number of times and then calculate the delay. However, this may not be ideal for my application.

    I'll appreciate if anyone knows of a particular function which can be used to determine the time delay. Thanks.

  5. #5
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I'm not sure of it's exact name, but it's something like "QueryPerformanceCounter" or something. I don't recall though and I don't know if it supports Linux. This function has precision to about 0.3 microseconds or so (or 300 nanoseconds). The standard clock seems to have a precision of 1/64 second (which is 15,625 microseconds) and thus not an option for your needs. It can be used with a little thinking - duplication works wonders. The Windows multimedia timer, of which is what I use, gets as precise as about 2000 milliseconds, still a bit much, but by copying a function a few thousand times, it can work this way, but that's not really all that feasible (though it takes about a minute for a test case). If you're just after what's faster between two methods, then you don't need high precision, but just a simple loop with the same criteria for that loop and only one thing being changed. It's part of running experiments.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The only realistic way to get high precision for such a short operation (I'm assuming that data is not hundreds of kilobytes) is to use timestamp counters.

    You can use the RDTSC instruction using the "rdtscll()" function - I'm not quite sure which header file it lives in - in the kernel its include/asm-i386/msr.h or include/asm-x86_64/msr.h

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Can I ask if anyone tried using the command "ftime"? I've tried to incorporate that in my program and it seems like the command could not be recognised.

    Code:
     
    #include <sys/timeb.h>
    
    struct timeb        startTime;
    struct timeb        endTime;
    time_t              deltaSecs;
    unsigned            deltaMsecs;
    
    ftime(&startTime);
    //
    // I tried to calculate the delay in processing this function here.
    //
    ftime(&endTime);
    
    deltaSecs  = endTime.time - startTime.time;
    
    if ( endTime.millitm < startTime.millitm  ) {
            deltaSecs--;
            deltaMsecs = 1000 + endTime.millitm - startTime.millitm;
        }
        else {
            deltaMsecs = endTime.millitm - startTime.millitm;
        }
    Errors during compilation:

    spi1867h.c:117: error: conflicting types for 'endTime'
    spi1867h.c:113: error: previous declaration of 'endTime' was here
    spi1867h.c:152: warning: passing arg 1 of `ftime' from incompatible pointer type
    spi1867h.c:170: error: request for member `time' in something not a structure or union
    spi1867h.c:171: error: request for member `millitm' in something not a structure or union
    spi1867h.c:173: error: request for member `millitm' in something not a structure or union
    spi1867h.c:176: error: request for member `millitm' in something not a structure or union

    Can I ask if I'm missing something here?

    Thanks for any help.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And where is line 113?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It says that ftime has been replaced by gettimeofday().

    However, you are doing something that is most likely going to take less than 1ms, and whilst getting nominal microseconds value from gettimeofday(), it is not certain to be a precise number [it may well give you microseconds in 1000 microsecond "lumps" between consecutive calls].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  3. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM