All pastes #425561 Raw Edit

burgerbee

public c v1 · immutable
·published 2007-04-05 15:33 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    96000#define SECS 20#define LTC   5/* number of cycles we are using per call */long long cycles_last = 0;long long cycles_worst = 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(j=0; j<SECS; j++) {        for(i=0; i<N; i++) {            start_cycles = cycles();            cycles_last = cycles() - start_cycles;            cycles_average += (cycles_last - cycles_average +(1<<(LTC-1))) >> LTC;            if (cycles_last > cycles_worst)                cycles_worst = cycles_last;        }    }    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 executes %5.2f MIPS\n\n", mips_cpu);    printf("Method 1: gettimeofday() at start and end\n");    printf("Time:\t%d ms\n", t_ms, SECS);    mips_per_ec = mips_cpu/((float)SECS*1E3/(float)t_ms);    printf("Mips:\t%5.2f\n\n", mips_per_ec);    printf("Method 2: samples clock cycles at start and end\n");    printf("Mips:\t%5.2f\n\n", (after_clocks - before_clocks)/(1E6*SECS));    printf("Method 3: samples clock cycles for each call\n");    mips_per_ec = 8*(float)cycles_average/1000;    printf("Cycles_worst:\t %lld\nCycles_last:\t %lld\nCycles_av:\t %lld"           "\nMips:\t\t%5.2f\n",           cycles_worst, cycles_last, cycles_average, mips_per_ec);    return 0;}