-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_size.cpp
More file actions
103 lines (95 loc) · 3.34 KB
/
test_size.cpp
File metadata and controls
103 lines (95 loc) · 3.34 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <cstdlib>
#include <iostream>
#include <iostream>
#include <sys/time.h>
#include "BlockAllocator.h"
#include "CAllocator.h"
#include "FreeListAllocator.h"
constexpr size_t MEMSIZE = ((size_t)78ul) << 30;
inline double get_time() {
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec * 1000.0 + t.tv_usec / 1000.0;
}
void test(Allocator *allocator) {
for (int i = 2; i < 28; ++i) {
for (int j = 0; j < 128; j++) {
void *ptr = allocator->Allocate(1 << i);
float *fptr = (float *)ptr;
for (int j = 0; j < i; j += 1 << (i - 2)) {
fptr[j] = j;
}
allocator->Free(ptr);
}
double time_alloc = 0, time_free = 0;
for (int j = 0; j < 128; j++) {
double t0 = get_time();
void *ptr = allocator->Allocate(1 << i);
double t1 = get_time();
float *fptr = (float *)ptr;
for (int j = 0; j < i; j += 1 << (i - 2)) {
fptr[j] = j;
}
double t2 = get_time();
allocator->Free(ptr);
double t3 = get_time();
time_alloc += t1 - t0;
time_free += t3 - t2;
}
std::cout << "size: " << (1 << i) << ", alloc: " << time_alloc / 128
<< "ms, "
<< " free: " << time_free / 128 << "ms." << std::endl;
}
}
void test(BlockAllocator *allocator) {
for (int i = 2; i < 28; ++i) {
for (int j = 0; j < 128; j++) {
void *ptr = allocator->Allocate(1 << i);
float *fptr = (float *)ptr;
for (int j = 0; j < i; j += 1 << (i - 2)) {
fptr[j] = j;
}
allocator->Free(ptr);
}
double time_alloc = 0, time_free = 0;
for (int j = 0; j < 128; j++) {
double t0 = get_time();
void *ptr = allocator->Allocate(1 << i);
double t1 = get_time();
float *fptr = (float *)ptr;
for (int j = 0; j < i; j += 1 << (i - 2)) {
fptr[j] = j;
}
double t2 = get_time();
allocator->Free(ptr);
double t3 = get_time();
time_alloc += t1 - t0;
time_free += t3 - t2;
}
std::cout << "size: " << (1 << i) << ", alloc: " << time_alloc / 128
<< "ms, "
<< " free: " << time_free / 128 << "ms." << std::endl;
}
}
int main() {
Allocator *callocator = new CAllocator();
Allocator *ffallocator = new FreeListAllocator(
MEMSIZE, FreeListAllocator::PlacementPolicy::FIND_FIRST);
Allocator *fballocator = new FreeListAllocator(
MEMSIZE, FreeListAllocator::PlacementPolicy::FIND_BEST);
BlockAllocator *block_allocator = new BlockAllocator(MEMSIZE);
ffallocator->Init();
fballocator->Init();
std::cout << "Testing CAllocator: " << std::endl;
test(callocator);
std::cout << "Testing FreeListAllocator with Find_First: " << std::endl;
test(ffallocator);
std::cout << "Testing FreeListAllocator with Find_Best: " << std::endl;
test(fballocator);
std::cout << "Testing BlockAllocator: " << std::endl;
test(block_allocator);
std::cout << "Testing BlockAllocator without malloc: " << std::endl;
block_allocator->flag_malloc = false;
test(block_allocator);
return 0;
}