All pastes #434924 Raw Edit

burgerbee

public c v1 · immutable
·published 2007-04-11 18:02 UTC
rendered paste body
/* speedtest.c - speedtest */#include <stdlib.h>#include <stdio.h>#include <sys/time.h>#include <time.h>#include <string.h>#include <unistd.h>#define N   10E6#if defined(__i386__)static __inline__ unsigned long long cycles(void) {  unsigned long long int x;     __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));     return x;}#elif defined(__x86_64__)static __inline__ unsigned long long cycles(void) {  unsigned hi, lo;  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));  return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );}#elif defined(__powerpc__)static __inline__ unsigned long long cycles(void) {  unsigned long long int result=0;  unsigned long int upper, lower,tmp;  __asm__ volatile(                "0:                  \n"                "\tmftbu   %0           \n"                "\tmftb    %1           \n"                "\tmftbu   %2           \n"                "\tcmpw    %2,%0        \n"                "\tbne     0b         \n"                : "=r"(upper),"=r"(lower),"=r"(tmp)                );  result = upper;  result = result<<32;  result = result|lower;  return(result);}#endifint main(int argc, char **argv) {    int                i, attempt, t_ms;    struct timeval     tv_before;    struct timeval     tv_after;    unsigned long long t_before_ms, t_after_ms;    unsigned long long before_clocks, after_clocks;    unsigned long long start_cycles;    for (attempt=0; attempt<3;attempt++) {        gettimeofday(&tv_before, NULL);        before_clocks = cycles();        for(i=0; i<N; i++) {            start_cycles = cycles();        }        after_clocks = cycles();        gettimeofday(&tv_after, NULL);        t_before_ms = 1000*tv_before.tv_sec + tv_before.tv_usec/1000;        t_after_ms = 1000*tv_after.tv_sec + tv_after.tv_usec/1000;        t_ms = t_after_ms - t_before_ms;        printf("10E6 RDTSC time=%d ms\n", t_ms);    }    return 0;}