Skip to content

Instantly share code, notes, and snippets.

@bendlas
Created March 8, 2011 21:05
Show Gist options
  • Save bendlas/861055 to your computer and use it in GitHub Desktop.
Save bendlas/861055 to your computer and use it in GitHub Desktop.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
// defined in asma.s
unsigned char *asma(unsigned char *s);
unsigned char *asma_ref(unsigned char *s)
{
int i;
for (i=0; i<16; i++) {
unsigned char c = s[i];
c += (c >= 'A' && c <= 'Z') ? 'a' - 'A' : 0;
s[i] = c;
}
return s;
}
void genrand(unsigned char *arr16){
for (int i=0; i<4; ++i) {
((uint32_t*) arr16)[i] = rand();
}
}
int main(int argc, char **argv) {
unsigned char src[17], tmp_ref[17], tmp[17];
src[16] = 0; tmp_ref[16] = 0; tmp[16] = 0;
srand((unsigned int) time(NULL));
int success_count = 0;
struct timespec clk_b, clk_e;
long ref_time=0, my_time=0;
for (int i=0; i<200000; ++i) {
genrand(src);
memcpy(tmp, src, 16);
memcpy(tmp_ref, src, 16);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &clk_b);
asma(tmp);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &clk_e);
my_time += clk_e.tv_nsec - clk_b.tv_nsec;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &clk_b);
asma_ref(tmp_ref);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &clk_e);
ref_time += clk_e.tv_nsec - clk_b.tv_nsec;
if (memcmp(tmp, tmp_ref, 16)) {
printf("Error: #%d: results don't match\n", i);
}
else {
// printf("Source: %s\nMine: %s\nRef: %s\n", src, tmp, tmp_ref);
++success_count;
}
}
printf("%d/200000 examples successful\n", success_count);
double n = 1000000000;
printf("ref: %f sec\nme: %f sec\n %f%%\n", ref_time/n, my_time/n, (((double) my_time) - ((double) ref_time)) * 100 / ref_time);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment