-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
111 lines (93 loc) · 3.4 KB
/
benchmark.cpp
File metadata and controls
111 lines (93 loc) · 3.4 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
104
105
106
107
108
109
110
111
#include "my_colony.hpp"
#include <algorithm>
#include <chrono>
#include <iostream>
#include <list>
#include <string>
#include <vector>
// The data structure we are testing
struct MyStruct {
std::string name;
int age;
bool field1;
char *field2;
};
// Simple helper to get time in milliseconds
double get_ms(std::chrono::high_resolution_clock::time_point start) {
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration<double, std::milli>(end - start).count();
}
int main() {
// CSV Header
std::cout << "Size,Container,Operation,TimeMS" << std::endl;
// We will test these sizes to see the performance curve
std::vector<int> test_sizes = {100000, 200000, 400000,
600000, 800000, 1000000};
for (int NUM : test_sizes) {
// ==========================================
// 1. std::vector
// ==========================================
{
std::vector<MyStruct> v;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < NUM; ++i)
v.push_back({"test", i, true, nullptr});
std::cout << NUM << ",Vector,Insertion," << get_ms(start) << "\n";
start = std::chrono::high_resolution_clock::now();
long long sum = 0;
for (const auto &item : v)
sum += item.age;
volatile long long bh = sum;
std::cout << NUM << ",Vector,Iteration," << get_ms(start) << "\n";
start = std::chrono::high_resolution_clock::now();
v.erase(std::remove_if(v.begin(), v.end(),
[](const MyStruct &s) { return s.age % 2 == 0; }),
v.end());
std::cout << NUM << ",Vector,Erasure," << get_ms(start) << "\n";
}
// ==========================================
// 2. std::list
// ==========================================
{
std::list<MyStruct> l;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < NUM; ++i)
l.push_back({"test", i, true, nullptr});
std::cout << NUM << ",List,Insertion," << get_ms(start) << "\n";
start = std::chrono::high_resolution_clock::now();
long long sum = 0;
for (const auto &item : l)
sum += item.age;
volatile long long bh = sum;
std::cout << NUM << ",List,Iteration," << get_ms(start) << "\n";
start = std::chrono::high_resolution_clock::now();
l.remove_if([](const MyStruct &s) { return s.age % 2 == 0; });
std::cout << NUM << ",List,Erasure," << get_ms(start) << "\n";
}
// ==========================================
// 3. my_lib::Colony
// ==========================================
{
my_lib::Colony<MyStruct> c;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < NUM; ++i)
c.insert({"test", i, true, nullptr});
std::cout << NUM << ",Colony,Insertion," << get_ms(start) << "\n";
start = std::chrono::high_resolution_clock::now();
long long sum = 0;
for (const auto &item : c)
sum += item.age;
volatile long long bh = sum;
std::cout << NUM << ",Colony,Iteration," << get_ms(start) << "\n";
start = std::chrono::high_resolution_clock::now();
for (auto it = c.begin(); it != c.end();) {
if (it->age % 2 == 0)
it = c.erase(it);
else
++it;
}
std::cout << NUM << ",Colony,Erasure," << get_ms(start) << "\n";
}
}
return 0;
}