Created
July 30, 2012 10:32
-
-
Save vmg/3206065 to your computer and use it in GitHub Desktop.
SYNTHETIC BENCHMARKS ARE HARD LET'S GO SHOPPING
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <sys/time.h> | |
#include <sys/resource.h> | |
static double get_time() | |
{ | |
struct timeval t; | |
struct timezone tzp; | |
gettimeofday(&t, &tzp); | |
return t.tv_sec + t.tv_usec*1e-6; | |
} | |
#define GIT_OID_RAWSZ 20 | |
typedef struct _git_oid git_oid; | |
struct _git_oid { | |
/** raw binary formatted id */ | |
unsigned char id[GIT_OID_RAWSZ]; | |
}; | |
static inline int git_oid_naive(const git_oid *a, const git_oid *b) | |
{ | |
return memcmp(a->id, b->id, GIT_OID_RAWSZ); | |
} | |
static inline int git_oid_cmp1(const git_oid *a, const git_oid *b) | |
{ | |
const unsigned char *sha1 = a->id; | |
const unsigned char *sha2 = b->id; | |
int i; | |
for (i = 0; i < GIT_OID_RAWSZ; i++, sha1++, sha2++) { | |
if (*sha1 != *sha2) | |
return *sha1 - *sha2; | |
} | |
return 0; | |
} | |
static inline int git_oid_cmp2(const git_oid *a, const git_oid *b) | |
{ | |
const uint32_t *sha1 = (uint32_t *)a->id; | |
const uint32_t *sha2 = (uint32_t *)b->id; | |
int i; | |
#ifdef __GNUC__ | |
if (__builtin_expect(*sha1 != *sha2, 1)) | |
#else | |
if (*sha1 != *sha2) | |
#endif | |
return *sha1 - *sha2; | |
for (i = 1; i < GIT_OID_RAWSZ / sizeof(uint32_t); ++i) { | |
if (sha1[i] != sha2[i]) | |
return sha1[i] - sha2[i]; | |
} | |
return 0; | |
} | |
static inline int git_oid_cmp3(const git_oid *a, const git_oid *b) | |
{ | |
const uint32_t *sha1 = (uint32_t *)a->id; | |
const uint32_t *sha2 = (uint32_t *)b->id; | |
return sha1[0] - sha2[0] || | |
sha1[1] - sha2[1] || | |
sha1[2] - sha2[2] || | |
sha1[3] - sha2[3] || | |
sha1[4] - sha2[4]; | |
} | |
static inline int git_oid_cmp4(const git_oid *a, const git_oid *b) | |
{ | |
const uint32_t *sha1 = (uint32_t *)a->id; | |
const uint32_t *sha2 = (uint32_t *)b->id; | |
return sha1[0] - sha2[0] | | |
sha1[1] - sha2[1] | | |
sha1[2] - sha2[2] | | |
sha1[3] - sha2[3] | | |
sha1[4] - sha2[4]; | |
} | |
#define OID_CMP(a, b) git_oid_cmp2(a, b) | |
#define OID_COUNT (32000) | |
static git_oid *oid_array; | |
static void build_corpus() | |
{ | |
size_t i, j; | |
srand(4225012); | |
oid_array = malloc(OID_COUNT * sizeof(git_oid)); | |
for (i = 0; i < OID_COUNT; ++i) { | |
git_oid *oid = &oid_array[i]; | |
for (j = 0; j < GIT_OID_RAWSZ; ++j) | |
oid->id[j] = rand() & 0xff; | |
} | |
} | |
static size_t test_run() | |
{ | |
size_t i, j, x = 0; | |
for (i = 0; i < OID_COUNT; ++i) { | |
git_oid *a = &oid_array[i]; | |
for (j = 0; j < OID_COUNT; ++j) { | |
git_oid *b = &oid_array[j]; | |
x |= OID_CMP(a, b); | |
} | |
} | |
return x; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
static int runs = 8; | |
int i; | |
double total = 0; | |
size_t x = 0; | |
build_corpus(); | |
for (i = 0; i < runs; ++i) { | |
double start, end; | |
start = get_time(); | |
x |= test_run(); | |
end = get_time(); | |
printf("Run %d: %fs\n", i + 1, end - start); | |
total += end - start; | |
} | |
printf("AVG: %f\n", total / runs); | |
return x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment