-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_testcase2.c
More file actions
executable file
·73 lines (67 loc) · 1.69 KB
/
advanced_testcase2.c
File metadata and controls
executable file
·73 lines (67 loc) · 1.69 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
69
70
71
72
73
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "537malloc.h"
#define LIMIT 100000
#define SIZE_RANGE 10000
int main() {
int i;
int mem_size[LIMIT];
int *addr[LIMIT] = {NULL};
// Getting random sizes
srand(time(NULL));
for(i = 0; i < LIMIT; i++) {
mem_size[i] = rand() % SIZE_RANGE + 1;
}
clock_t startTimeAlloc = clock();
printf("Allocating randomly of random size\n");
// Allocating randomly
int count = 0, index;
while(count < LIMIT) {
index = rand() % LIMIT;
if(addr[index] == NULL) {
addr[index] = malloc537(mem_size[index]);
printf("Allocated %d size @ %p\n", mem_size[index], addr[index]);
}
count++;
}
// fill out the missing allocations
count = 0;
while(count < LIMIT) {
if(addr[count] == NULL) {
addr[count] = malloc537(mem_size[count]);
printf("Allocated %d size @ %p\n", mem_size[count], addr[count]);
}
count++;
}
clock_t endTimeAlloc = clock();
printf("Freeing randomly from the allocated addresses\n");
clock_t startTimeFree = clock();
// free randomly
count = 0;
index = 0;
while(count < LIMIT) {
index = rand() % LIMIT;
if(addr[index] != NULL) {
printf("Free memory @ %p\n", addr[index]);
free537(addr[index]);
addr[index] = NULL;
}
count++;
}
// free the rest
count = 0;
while(count < LIMIT) {
if(addr[count] != NULL) {
printf("Free memory @ %p\n", addr[count]);
free537(addr[count]);
addr[index] = NULL;
}
count++;
}
clock_t endTimeFree = clock();
printf("If this prints, you get points!\n");
printf("Alloc Time Taken : %f secs\n", ((double)endTimeAlloc - startTimeAlloc) / CLOCKS_PER_SEC);
printf("Free Time Taken : %f secs\n", ((double)endTimeFree - startTimeFree) / CLOCKS_PER_SEC);
return 0;
}