Last active
July 20, 2017 12:47
-
-
Save kristerw/f84686c1821dbb81c41e8b97dbef9ef9 to your computer and use it in GitHub Desktop.
Benchmarking code for https://kristerw.blogspot.se/2017/07/a-loadstore-performance-corner-case.html
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 <benchmark/benchmark.h> | |
unsigned char *init_test_memset(int); | |
unsigned char *init_test_random(int); | |
void run_test(unsigned char *, int); | |
void release_test(unsigned char *); | |
static void BM_cleared(benchmark::State &state) | |
{ | |
const int len = state.range(0); | |
unsigned char *p = init_test_memset(len); | |
while (state.KeepRunning()) | |
{ | |
run_test(p, len); | |
} | |
release_test(p); | |
} | |
static void BM_random(benchmark::State &state) | |
{ | |
const int len = state.range(0); | |
unsigned char *p = init_test_random(len); | |
while (state.KeepRunning()) | |
{ | |
run_test(p, len); | |
} | |
release_test(p); | |
} | |
BENCHMARK(BM_cleared)->Arg(1 << 12); | |
BENCHMARK(BM_random)->Arg(1 << 12); | |
BENCHMARK_MAIN(); |
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 <stdlib.h> | |
#include <string.h> | |
int histogram[256]; | |
unsigned char *init_test_memset(int len) | |
{ | |
unsigned char *p = (unsigned char *)malloc(len); | |
if (p == NULL) | |
exit(1); | |
memset(p, 0, len); | |
return p; | |
} | |
unsigned char *init_test_random(int len) | |
{ | |
unsigned char *p = (unsigned char *)malloc(len); | |
if (p == NULL) | |
exit(1); | |
for (int i = 0; i < len; i++) | |
p[i] = rand(); | |
return p; | |
} | |
void run_test(unsigned char *p, int len) | |
{ | |
memset(histogram, 0, sizeof(histogram)); | |
for (int i = 0; i < len; i++) | |
histogram[p[i]]++; | |
} | |
void release_test(unsigned char *p) | |
{ | |
free(p); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment