-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_benchmark.c
More file actions
68 lines (56 loc) · 1.32 KB
/
memory_benchmark.c
File metadata and controls
68 lines (56 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdlib.h>
#include <string.h>
#include <wait.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
double doForks();
double doVForks();
int main(int argc, char *argv[])
{
long mallocSize = 100 * 1024 * 1024;
void *ptr = malloc(mallocSize);
memset(ptr, 0, mallocSize);
printf("Time taken per fork (100MB):\t%f\n", doForks());
printf("Time taken per vfork (100MB):\t%f\n", doVForks());
free(ptr);
ptr = malloc(mallocSize * 5);
memset(ptr, 0, mallocSize * 5);
printf("Time taken per fork (500MB):\t%f\n", doForks());
printf("Time taken per vfork (500MB):\t%f\n", doVForks());
free(ptr);
ptr = malloc(mallocSize * 10);
memset(ptr, 0, mallocSize * 10);
printf("Time taken per fork (1000MB):\t%f\n", doForks());
printf("Time taken per vfork (1000MB):\t%f\n", doVForks());
free(ptr);
return 0;
}
double doForks()
{
clock_t start = clock();
pid_t child = fork();
if (child)
{
waitpid(child, NULL, 0);
}
else
{
exit(0);
}
return (((double)(clock() - start)) / CLOCKS_PER_SEC);
}
double doVForks()
{
clock_t start = clock();
pid_t child = vfork();
if (child)
{
waitpid(child, NULL, 0);
}
else
{
_exit(0);
}
return (((double)(clock() - start)) / CLOCKS_PER_SEC);
}