Thread: clock cycle counter?

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    }
    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

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, 06:36 PM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06: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, 07:16 PM
  5. clock and cycle times' effect on counter time?
    By rexnprogress in forum C++ Programming
    Replies: 0
    Last Post: 11-11-2005, 07:16 PM