-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
82 lines (65 loc) · 1.84 KB
/
test.cpp
File metadata and controls
82 lines (65 loc) · 1.84 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
#include "PoolAlloc.hpp"
#include "StackAlloc.hpp"
#include "StdWrapper.hpp"
#include "GeneralAlloc.hpp"
#include <iostream>
#include <vector>
#include <list>
#include <chrono>
using namespace moe;
using namespace moe::memory;
using namespace moe::errors;
class TestClass {
public:
TestClass(const std::string& name, float x = 0.f, float y = 0.f)
: _name{ name }, _x{ x }, _y{ y }
{ /*std::cout << _name << " constructed." << std::endl;*/ }
~TestClass() { /*std::cout << _name << " destructed." << std::endl;*/ }
void test() {
std::cout << "Hi, my name is " << _name << std::endl;
}
private:
moe_string _name;
float _x;
float _y;
};
int main(int argc, char** args) {
typedef std::chrono::high_resolution_clock clock;
typedef std::chrono::duration<double, std::milli> tick;
const size_t ITS = 400000;
TestClass* objs1[ITS];
TestClass* objs2[ITS];
TestClass* objs3;
PoolAlloc<TestClass> pool{ ITS };
StackAlloc stack{ ITS * sizeof(TestClass) };
for (size_t j = 0; j < 10; j++) {
try {
//run 1: use new and delete:
auto t0 = clock::now();
for (size_t i = 0; i < ITS; i++) {
objs1[i] = (new TestClass{ "test" });
}
for (size_t i = 0; i < ITS; i++) delete objs1[i];
auto dt = tick(clock::now() - t0).count();
std::cout << "New and delete: " << dt << std::endl;
t0 = clock::now();
//run 2: use poolAlloc:
for (size_t i = 0; i < ITS; i++) {
objs2[i] = pool.make("test");
}
for (size_t i = 0; i < ITS; i++) pool.destroy(objs2[i]);
dt = tick(clock::now() - t0).count();
std::cout << "Pool Alloc: " << dt << std::endl;
t0 = clock::now();
objs3 = stack.make<TestClass>(ITS, "test");
stack.destroyAll();
dt = tick(clock::now() - t0).count();
std::cout << "StackAlloc: " << dt << std::endl;
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return -1;
}
}
return 0;
}