Skip to content

Instantly share code, notes, and snippets.

@RedBeard0531
Created November 17, 2010 16:36
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 RedBeard0531/703610 to your computer and use it in GitHub Desktop.
Save RedBeard0531/703610 to your computer and use it in GitHub Desktop.
/* test.c */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifndef _WIN32
#include <sys/time.h>
#endif
static char hexbyte1(char hex){
switch (hex){
case '0': return 0x0;
case '1': return 0x1;
case '2': return 0x2;
case '3': return 0x3;
case '4': return 0x4;
case '5': return 0x5;
case '6': return 0x6;
case '7': return 0x7;
case '8': return 0x8;
case '9': return 0x9;
case 'a':
case 'A': return 0xa;
case 'b':
case 'B': return 0xb;
case 'c':
case 'C': return 0xc;
case 'd':
case 'D': return 0xd;
case 'e':
case 'E': return 0xe;
case 'f':
case 'F': return 0xf;
default: return 0x0; /* something smarter? */
}
}
static char hexbyte2(char hex) {
char rv = hex - '0'; // handles '0' .. '9'
if (rv > 9) rv -= 7; // handles 'A' .. 'F'
return rv & 0xF; // handles 'a' .. 'f'
}
volatile char x;
void test1(){
int i = 100000000;
while(i--)
x = hexbyte1('A'+i%6);
}
void test2(){
int i = 100000000;
while(i--)
x = hexbyte2('A'+i%6);
}
typedef void(*nullary)();
static void time_it(nullary func, const char* name){
double timer;
#ifdef _WIN32
int64_t start, end;
start = GetTickCount64();
func();
end = GetTickCount64();
timer = end - start;
#else
struct timeval start, end;
gettimeofday(&start, NULL);
func();
gettimeofday(&end, NULL);
timer = end.tv_sec - start.tv_sec;
timer *= 1000000;
timer += end.tv_usec - start.tv_usec;
timer /= 1000000;
#endif
printf("%-45s\t%15f\n", name, timer);
}
#define TIME(func) (time_it(func, #func))
int main(){
TIME(test1);
TIME(test2);
TIME(test1);
TIME(test2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment