-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
66 lines (55 loc) · 1.56 KB
/
main.cpp
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
#include<iostream>
#include<cassert>
#include<ctime>
#include<vector>
#include"StackAlloc.hpp"
#include"MemoryPool.hpp"
#define ELEMS 1e5
#define REPS 100
int main()
{
clock_t start;
//use stl default allocator
StackAlloc<int,std::allocator<int>> stackDefault;
start = clock();
for(int i = 0; i< REPS;i++)
{
//非空时会引起中断
assert(stackDefault.empty());
for(int j = 0;j<ELEMS;j++)
stackDefault.push(j);
for(int j = 0;j<ELEMS;j++)
stackDefault.pop();
}
std::cout<<"Default Allocator Time:";
std::cout<<(((double)clock()-start)/CLOCKS_PER_SEC)<<std::endl;
//use stl vector
std::vector<int> stackVector;
start = clock();
for(int i = 0; i< REPS;i++)
{
//非空时会引起中断
assert(stackVector.empty());
for(int j = 0;j<ELEMS;j++)
stackVector.push_back(j);
for(int j = 0;j<ELEMS;j++)
stackVector.pop_back();
}
std::cout<<"Vector Allocator Time:";
std::cout<<(((double)clock()-start)/CLOCKS_PER_SEC)<<std::endl;
//use MemoryPool
StackAlloc<int,MemoryPool<int>> stackPool;
start = clock();
for(int i = 0; i< REPS;i++)
{
//非空时会引起中断
assert(stackPool.empty());
for(int j = 0;j<ELEMS;j++)
stackPool.push(j);
for(int j = 0;j<ELEMS;j++)
assert(stackPool.pop()==ELEMS-j-1);
}
std::cout<<"MemoryPool Allocator Time:";
std::cout<<(((double)clock()-start)/CLOCKS_PER_SEC)<<std::endl;
return 0;
}