#include #include #include #include #define SIZE (64 << 20) unsigned tv_msecs(struct timeval *tvs, struct timeval *tve) { return (tve->tv_sec - tvs->tv_sec) * 1000 + (tve->tv_usec - tvs->tv_usec) / 1000; } int main(void) { struct timeval tvs, tve; void *buf; int *p, *e, i; gettimeofday(&tvs, NULL); buf = malloc(SIZE); gettimeofday(&tve, NULL); printf("malloc took %d msecs\n", tv_msecs(&tvs, &tve)); gettimeofday(&tvs, NULL); for (p = buf, e = buf + SIZE; p < e; p += 0x1000) i = *(volatile int*)p; gettimeofday(&tve, NULL); printf("read took %d msecs\n", tv_msecs(&tvs, &tve)); gettimeofday(&tvs, NULL); for (p = buf, e = buf + SIZE; p < e; p += 0x1000) *(volatile int*)p = 0xaa55aa55; gettimeofday(&tve, NULL); printf("write took %d msecs\n", tv_msecs(&tvs, &tve)); gettimeofday(&tvs, NULL); free(buf); gettimeofday(&tve, NULL); printf("free took %d msecs\n", tv_msecs(&tvs, &tve)); gettimeofday(&tvs, NULL); buf = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0); gettimeofday(&tve, NULL); printf("mmap took %d msecs\n", tv_msecs(&tvs, &tve)); gettimeofday(&tvs, NULL); munmap(buf, SIZE); gettimeofday(&tve, NULL); printf("munmap took %d msecs\n", tv_msecs(&tvs, &tve)); return 0; }