Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/time.h>
- #include <sys/mman.h>
- #include <string.h>
- #include <stdlib.h>
- double mydifftime(struct timeval *tv1, struct timeval *tv2)
- {
- return ((tv1->tv_sec - tv2->tv_sec) * 1000000.0 +
- (tv1->tv_usec - tv2->tv_usec)) / 1000000.0;
- }
- int main()
- {
- int MMAP_SIZE = 1<<20;
- int i;
- printf(" size \tmmap time\tmunmap time\tt1/t2 \tt2/size\n");
- for (i = 0; i < 11; i++, MMAP_SIZE <<= 1) {
- struct timeval tv1, tv2;
- double t1, t2;
- gettimeofday(&tv1, NULL);
- void *addr = mmap(NULL, MMAP_SIZE, PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
- gettimeofday(&tv2, NULL);
- if (addr == MAP_FAILED)
- abort();
- t1 = mydifftime(&tv2, &tv1);
- /* commit memory */
- memset(addr, 1, MMAP_SIZE);
- gettimeofday(&tv1, NULL);
- munmap(addr, MMAP_SIZE);
- gettimeofday(&tv2, NULL);
- t2 = mydifftime(&tv2, &tv1);
- printf("%*d\t%f\t%f\t%f\t%f\n", 10, MMAP_SIZE, t1, t2, t2/t1,
- t2*100000000.0/(MMAP_SIZE));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement