Skip to content

Instantly share code, notes, and snippets.

@mrsmith
Created May 11, 2012 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrsmith/2660072 to your computer and use it in GitHub Desktop.
Save mrsmith/2660072 to your computer and use it in GitHub Desktop.
Simple cache-size detecting test
#include <stdio.h>
#include <malloc.h>
#include <sys/time.h>
#include <assert.h>
#define TIMER(t) double (t) = 0.0
#define START_TIMER(t) do { \
struct timeval start; \
gettimeofday(&start, 0); \
(t) = (double) start.tv_sec + (double) start.tv_usec / 1e6; \
} while (0)
#define STOP_TIMER(t) do { \
struct timeval stop;\
gettimeofday(&stop, 0); \
(t) = (double) stop.tv_sec + (double) stop.tv_usec / 1e6 - (t);\
} while (0)
#define MS(t) ((t) * 1e3)
const size_t CAHCHELINE_SIZE = 64;
int main(int argc, char **argv)
{
const size_t MAX_DATA_SIZE = 1024 * 1024 * 512;
const size_t ACCESS_TIMES = 1024 * 1024 * 64;
char *data = memalign(CAHCHELINE_SIZE, MAX_DATA_SIZE);
assert(data);
for (size_t data_size = 256; data_size < MAX_DATA_SIZE; data_size *= 2) {
printf("%zu", data_size);
TIMER(t); START_TIMER(t);
for (size_t i = 0; i < ACCESS_TIMES; ++i) {
size_t j = (i * 268435813L) % data_size;
data[j] = data[j] * 2;
}
STOP_TIMER(t);
printf("\t%.8f\n", t);
}
free(data);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment