Skip to content

Instantly share code, notes, and snippets.

@mattsta
Created December 20, 2014 19:54
Show Gist options
  • Save mattsta/1dcdaf8c434549892d0b to your computer and use it in GitHub Desktop.
Save mattsta/1dcdaf8c434549892d0b to your computer and use it in GitHub Desktop.
benchmark memory operations from small 32 bytes to 4+ MB. results reported as factional nanoseconds.
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
static long long ustime(void) {
struct timeval tv;
long long ust;
gettimeofday(&tv, NULL);
ust = ((long long)tv.tv_sec)*1e6;
ust += tv.tv_usec;
return ust;
}
#define MB_8 8388608
#define MB_32 MB_8 * 4
#define MB_64 MB_32 * 2
#define MB_4 MB_8/2
int main(int argc, char *argv[]) {
size_t limit = 32000;
char *thingOne = malloc(MB_64 + MB_4);
for (size_t size = 32; size <= MB_64; size *= 2) {
long long start = ustime();
for (size_t i = 0; i < limit; i++) {
memmove(thingOne+8, thingOne+32, size);
}
long long end = ustime();
long long duration_ns = (end - start) * 1000;
double individual_loop_ns = duration_ns / (double)limit;
double individual_memmove_time = individual_loop_ns / limit;
printf("%lu\t%0.4f\n", size, individual_memmove_time);
fflush(stdout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment