Skip to content

Instantly share code, notes, and snippets.

@flgr
Last active December 18, 2015 18:09
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 flgr/5823977 to your computer and use it in GitHub Desktop.
Save flgr/5823977 to your computer and use it in GitHub Desktop.
Simplistic way of measuring memcpy() performance on Unix systems.
#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main() {
struct timeval t0, t1;
unsigned int i;
unsigned int mb_size = 64;
unsigned int rep = 200;
for (unsigned int mb_size = 64; mb_size <= 1024; mb_size *= 2) {
size_t size = mb_size * 1024 * 1024;
void *src = malloc(size);
void *dst = malloc(size);
gettimeofday(&t0, NULL);
for (int i = 0; i < rep; i++) {
memcpy(src, dst, size);
}
gettimeofday(&t1, NULL);
free(src);
free(dst);
printf("Copied %d MB in %.2g seconds\n", mb_size, (t1.tv_sec - t0.tv_sec + 1E-6 * (t1.tv_usec - t0.tv_usec)) / rep);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment