Create a gist now

Instantly share code, notes, and snippets.

@kristerw /main.cpp
Last active Jul 20, 2017

What would you like to do?
#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();
#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