clock cycle counter?

This is a discussion on clock cycle counter? within the C Programming forums, part of the General Programming Boards category; Is there some function in c under linux that one can use to count clock cycles/nr of ticks? or similar? ...

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

    clock cycle counter?

    Is there some function in c under linux that one can use to count clock cycles/nr of ticks? or similar? preferably in nano secs.

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    You'll never have nanoseconds precision, well not with the computer we are using these days.

    I couldn't say if there's such function on Linux. I bet there is; someone else might answer you.

    But if you can write assembly for IA-32 processor (or Intel 64 architecture processor), then you should look at the RDTSC (ReaD Time Stamp Counter) instruction.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    somthing like
    Code:
    inline unsigned long rdtsc()
    {
    #if defined(__ia64)
        /* for Intel Itanium 64 bit processor */
        unsigned long x;
        __asm__ __volatile__("mov %0=ar.itc" : "=r"(x) :: "memory");
        while (__builtin_expect ((int) x == -1, 0))
            __asm__ __volatile__("mov %0=ar.itc" : "=r"(x) :: "memory");
        return x;
    #else
        /* for all others */
        unsigned long long x;
        unsigned int low,high;
        __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high));
        x = high;
        x = (x<<32)+low;
        return x;
    #endif
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 05:36 PM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 05:15 PM
  3. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  4. using clock()
    By sl4nted in forum C Programming
    Replies: 8
    Last Post: 11-09-2006, 06:16 PM
  5. clock and cycle times' effect on counter time?
    By rexnprogress in forum C++ Programming
    Replies: 0
    Last Post: 11-11-2005, 06:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21