Advertisement
Guest User

nibble sort timing harness

a guest
Jan 25th, 2015
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <time.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8.  
  9. #include "nibble.h"
  10.  
  11. static long timediff(struct timespec start, struct timespec end) {
  12.   struct timespec temp;
  13.   if ((end.tv_nsec-start.tv_nsec)<0) {
  14.     temp.tv_sec = end.tv_sec-start.tv_sec - 1;
  15.     temp.tv_nsec = 1000000000L + end.tv_nsec-start.tv_nsec;
  16.   } else {
  17.     temp.tv_sec = end.tv_sec-start.tv_sec;
  18.     temp.tv_nsec = end.tv_nsec-start.tv_nsec;
  19.   }
  20.   return temp.tv_sec * 1000000000L + temp.tv_nsec;
  21. }
  22.  
  23. static const int REPS = 25;
  24. static const int BUFSIZE = 1024;
  25.  
  26. typedef void (*nibble_func_t)(unsigned long *buf);
  27. typedef struct { nibble_func_t func; const char *name; } nibble_sort_t;
  28. static nibble_sort_t funcs[] = {
  29.   { nibble_sort_ref, "ref" },
  30.   { nibble_sort_payer, "payer" },
  31.   { nibble_sort_anon, "anon" },
  32.   { nibble_sort_alexander1, "alexander1" },
  33.   { nibble_sort_alexander2, "alexander2" },
  34.   { nibble_sort_pdewacht, "pdewacht" },
  35.   { nibble_sort_carlos, "carlos" },
  36.   { nibble_sort_burton, "burton" },
  37.   { nibble_sort_rogers, "rogers" },
  38. };
  39.  
  40. static unsigned long *getbuf(void) {
  41.   unsigned long *p;
  42.   int res = posix_memalign((void **)&p, 4096, BUFSIZE*8);
  43.   assert(res == 0);
  44.   assert(p);
  45.   assert(((intptr_t)p & 0xfff) == 0);
  46.   return p;
  47. }
  48.  
  49. int main (void) {
  50.   assert(sizeof(unsigned long)==8);
  51.   srand(time(NULL) + getpid());
  52.  
  53.   unsigned long *random_data = getbuf();
  54.   unsigned long *buf = getbuf();
  55.   unsigned long *buf2 = getbuf();
  56.  
  57.   for (int i=0; i<BUFSIZE*8; ++i)
  58.     ((unsigned char *)random_data)[i] = rand() & 0xff;
  59.  
  60.   // validate results
  61.   memcpy(buf2, random_data, BUFSIZE*8);
  62.   nibble_sort_ref(buf2);
  63.   for (int func=0; func<sizeof(funcs)/sizeof(nibble_sort_t); ++func) {
  64.     memcpy(buf, random_data, BUFSIZE*8);
  65.     funcs[func].func(buf);
  66.     for (int i=0; i<BUFSIZE; ++i) {
  67.       if (buf[i] != buf2[i]) {
  68.         printf("oops expected %016lx at %d to sort to %016lx but got %016lx\n",
  69.                random_data[i], i, buf2[i], buf[i]);
  70.         assert(0);
  71.       }
  72.     }
  73.   }
  74.  
  75.   // get timings
  76.   for (int func=0; func<sizeof(funcs)/sizeof(nibble_sort_t); ++func) {
  77.     long times[REPS];
  78.     for (int i=0; i<REPS; ++i) {
  79.       memcpy(buf, random_data, BUFSIZE*8);
  80.       struct timespec start, end;
  81.       clock_gettime(CLOCK_MONOTONIC, &start);
  82.       funcs[func].func(buf);
  83.       clock_gettime(CLOCK_MONOTONIC, &end);
  84.       times[i] = timediff(start, end);
  85.     }
  86.     printf("%s: ", funcs[func].name);
  87.     for (int i=0; i<REPS; ++i) {
  88.       printf("%ld ", times[i]);
  89.     }
  90.     printf("\n");
  91.   }
  92.   return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement