All pastes #426226 Raw Edit

burgerbee

public c v1 · immutable
·published 2007-04-05 23:54 UTC
rendered paste body
/* speed.c - speedtest */#include <stdlib.h>#include <stdio.h>#include <sys/time.h>#include <time.h>#include <string.h>#include <unistd.h>#define N   10E6/* number of cycles we are using per call */long long cycles_last = 0;long long cycles_worst = 0;long long cycles_total = 0;long long cycles_average = 0;__inline__ unsigned long long cycles() {  unsigned long long x;  __asm__ volatile ("rdtsc\n\t" : "=A" (x));  return x;}int main(int argc, char **argv) {    int                i,j;    struct timeval     tv_before;    struct timeval     tv_after;    unsigned long long t_before_ms, t_after_ms;    unsigned long long before_clocks, after_clocks;    int                t_ms;    unsigned long long start_cycles;    float              mips_cpu, mips_per_ec;    gettimeofday(&tv_before, NULL);    before_clocks = cycles();    for(i=0; i<N; i++) {            start_cycles = cycles();            cycles_last = cycles() - start_cycles;            cycles_total += cycles_last;    }    cycles_average =  cycles_total / N;    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;    mips_cpu = (after_clocks - before_clocks)/(1E3*t_ms);    printf("CPUs:\t\t %5.2f MIPS\n", mips_cpu);    printf("Used time:\t %d ms\n", t_ms);    printf("Cycles_last:\t %lld\nCycles_avg:\t %lld\n", cycles_last, cycles_average);    return 0;}