COMPUTE_MHZ.C

Various small tools

compute_MHZ.c source

Project updated · 3 August 2008

Download raw compute_MHZ.c ↓


#include <stdio.h>
#include <sys/time.h>

__inline__ unsigned long long int rdtsc()
{
        unsigned long long int x;
        __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
        return x;
}

int
main ()
{
        struct timezone tz;
        struct timeval tvstart, tvstop;
        unsigned long long int cycles[2]; /* gotta be 64 bit */
        unsigned int microseconds; /* total time taken */

        bzero(&tz, sizeof(tz));

        /* Load this function a cached memory. */
        gettimeofday(&tvstart, &tz);

        /* Read TSC for the first time and get timestamp.  */
        cycles[0] = rdtsc();
        gettimeofday(&tvstart, &tz);

        /* We don't trust that this is any specific length of time.
           We don't even care how long time we'll sleep. */
        sleep(1);

        /* Read TSC for the second time and get timestamp.  */
        cycles[1] = rdtsc();
        gettimeofday(&tvstop, &tz);

        /* Compute the frequency.  */
        microseconds = ((tvstop.tv_sec-tvstart.tv_sec)*1000000) +
                (tvstop.tv_usec-tvstart.tv_usec);
        printf("%f MHz processor.\n",
                (float)(cycles[1]-cycles[0])/microseconds);

        return 0;
}
compute_MHZ.c sourceUTF-8 · LOGIX.CZ

Find in Logix

Search page titles and descriptions. Esc closes.

A few handy keys

A little Borland, a little early desktop, a corner of the web to call home.

Pick a directory from the menu or tree. Use Find when you know what you’re after.

Tab / Shift+Tab
Move between controls
↑ / ↓ in menus
Choose an entry
← / → in menus
Switch directories
/
Find a page
Esc
Close menu or dialog
F1
Open this help

Prefer a different atmosphere? View has blue, grey, and green palettes.

No commands to learn. Just follow your curiosity.