Skip to content

Instantly share code, notes, and snippets.

@aji
Created February 20, 2012 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aji/1868190 to your computer and use it in GitHub Desktop.
Save aji/1868190 to your computer and use it in GitHub Desktop.
cycler -- cycles per time
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#define BUFSIZE 2<<15
char buforig[BUFSIZE];
char bufcopy[BUFSIZE];
char testname[512] = "no active test";
clock_t start = 0;
int cycles = 0;
int randomize(char *buffer, size_t len)
{
FILE *urandom = fopen("/dev/urandom", "r");
if (urandom == NULL)
return -1;
if (fread(buffer, 1, len, urandom) != len)
return -1;
return 0;
}
int cycle(double secs, int (*cb)(const void*, const void*, size_t),
void *arg1, void *arg2, size_t num)
{
clock_t end;
start = clock();
end = start + (secs * CLOCKS_PER_SEC);
cycles = 0;
while (clock() < end && ++cycles) {
(*cb)(arg1, arg2, num);
}
return cycles;
}
void info(int parameter)
{
printf("[%5.1fs]%43s: %10d cycles\n",
(clock() - start) / (float)(CLOCKS_PER_SEC), testname, cycles);
}
int main(int argc, char *argv[])
{
double secs = 5;
if (randomize(buforig, BUFSIZE) < 0) {
fprintf(stderr, "couldn't read from /dev/urandom\n");
return 1;
}
memcpy(bufcopy, buforig, BUFSIZE);
if (argc > 1) {
secs = strtod(argv[1], NULL);
}
signal(SIGTSTP, info);
printf("cycling for %.2f seconds ..\n", secs);
sprintf(testname, "memcmp(%p, %p, %d)", buforig, buforig, BUFSIZE);
cycle(secs, memcmp, buforig, buforig, BUFSIZE);
info(0);
sprintf(testname, "memcmp(%p, %p, %d)", buforig, bufcopy, BUFSIZE);
cycle(secs, memcmp, buforig, bufcopy, BUFSIZE);
info(0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment