Thread: Timing code in C?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    47

    Timing code in C?

    How do I account for time in C? I mean if I have 20 lines of code and I want to know how long time it takes to execute them in nano secs, how do I get this time?

    Thanks
    /Micke

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want precise time, you need to read the processor time-stamp counter [assuming it's a recent x86 processor].

    gcc will do that using a macro like this:
    Code:
    #define rdtscll(val) \
          __asm__ __volatile__("rdtsc" : "=A" (val))
    val is a "long long" type.
    --
    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.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    47
    Thanks, sound good, hmm, but I'm kind of new to macros, how do I use that?

    like:

    Code:
    #define rdtscll(val) \
          __asm__ __volatile__("rdtsc" : "=A" (val))
    
    long long test;
    long long diff;
    
    test = rdtscll(val);
    
    //some code to time
    //some more code ...
    
    diff = rdtscll(val) - test;
    where diff is what I want?

    Or how do I use it?

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    47
    I know in solaris there is gethrtime(), is there something similar in linux?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    rdtsc reads the clock-cycles, so you need to "divide by nr of GHz" to get nanoseconds, for example.

    I'm not familiar with a "high res" timer in Linux.

    --
    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.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by micke_b View Post
    Code:
    #define rdtscll(val) \
          __asm__ __volatile__("rdtsc" : "=A" (val))
    
    long long test, test2;
    long long diff;
    
    rdtscll(test);
    
    //some code to time
    //some more code ...
    
    rdtscll(test2);
    diff = test2 - test;
    I believe it's like this. "val" is a parameter. Basically, it translates "val" in the macro with whatever you pass as argument.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, elysia has the right code. Sorry, I missed that part of the original question.

    --
    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.

  8. #8
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Be aware that if you are in a multitask environnement, the operating system switch from process to process every x amout of time and thus, if your 20 lines of code takes more than a certain y amout of time (let's say microseconds, but i really don't know) because there's loop or fonction call in it (or beause your CPU has been interrupt while it was executing this part of the code), well, you might have some "incorrect" results.

    It will still give you an idea. But if you really need nano-second precision, well, you are losing your time because you'll never really have it.

    But i guess if you only want to know how much time it takes to execute some 20 simples C instructions (which i guess would be under 100 ns on a decent x86 processor), well, guess you'll be mostly fine.

    Someone with more knowledge than me could talk more about this. Just wanted to raise the point.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, there are many problems with measuring time precisely in computers [mostly to do with "heisenbergs uncertainty theorem" - the more precisely you measure, the more you influence the execution, which in turn influences the measurement].

    Big errors in measurement comes from:
    First of all, you should consider OS task-switching. As long as your code runs reasonably quick and there isn't too much other load on the system, you should be fine.

    Another potential problem is switching between processors - each processor runs at it's own specific frequncy, so there is at least a theoretical risk of drift between the two processors, and of coruse if you start on one processor and finish on another, any difference between the two processors will be added(subtracted) to the timing you measure.

    And yes, 20 lines of "simple" C should run in less than 100ns on a modern processor.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM