Skip to content

Instantly share code, notes, and snippets.

@ilyakurdyukov
Created October 27, 2020 07:11
Show Gist options
  • Save ilyakurdyukov/2f72151c79278f39a7b74fb33541147f to your computer and use it in GitHub Desktop.
Save ilyakurdyukov/2f72151c79278f39a7b74fb33541147f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>
static int64_t get_time_usec() {
struct timeval time;
gettimeofday(&time, NULL);
return (int64_t)time.tv_sec * 1000000 + time.tv_usec;
}
int main(int argc, char **argv) {
int i, j; int64_t t1, t2, t3, t4;
for (i = 0; i < 3; i++) {
int n = 100 << 20; char *p;
t1 = get_time_usec();
p = malloc(n);
t2 = get_time_usec();
for (j = 0; j < n; j += 0x1000) p[j] = 12;
t3 = get_time_usec();
for (j = 0; j < n; j += 0x1000) p[j] = 34;
t4 = get_time_usec();
free(p);
printf("alloc = %.3fms, touch1 = %.3fms, touch2 = %.3fms\n", (t2-t1) * 0.001, (t3-t2) * 0.001, (t4-t3) * 0.001);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment