-
Notifications
You must be signed in to change notification settings - Fork 5
/
bench.cxx
93 lines (76 loc) · 3.17 KB
/
bench.cxx
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
#include <map>
#include <string>
#include <sstream>
#include <iostream>
#include "kult.hpp"
// our obj components
using namespace kult;
using name = component<'name',std::string>;
using counter = component<'cter',size_t>;
// our sample
#include <chrono>
#include <vector>
#include <iomanip>
int main( int argc, char **argv )
{
{
// construct an object
//std::cout << "Sizeof(empty obj)=" << sizeof(obj) << std::endl;
int obj1 = 1;
add<name>(obj1) = "obj1";
add<counter>(obj1) = 0;
//std::cout << "Sizeof(valid obj)=" << sizeof(obj1) << std::endl;
// benchmark its access time
{
std::cout << "Benchmarking get<T>(id) syntax... ";
std::chrono::microseconds seconds1, seconds2;
{
auto t_start = std::chrono::high_resolution_clock::now();
for( size_t i = 0; i < 200000000; ++i )
get<counter>(obj1)++;
seconds1 = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - t_start);
}
size_t &ref = get<counter>(obj1);
{
auto t_start = std::chrono::high_resolution_clock::now();
for( size_t i = 0; i < 200000000; ++i )
ref++;
seconds2 = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - t_start);
}
double relative_speed = double( seconds1.count() ) / seconds2.count();
std::cout << "relative access is x" << std::fixed << std::setprecision(2) << relative_speed << " times " << ( seconds1 >= seconds2 ? "slower" : "faster" ) << " than direct access" << std::endl;
}
// debug obj
std::cout << dump(obj1) << std::endl;
}
{
// obj
kult::entity obj;
kult::component<'cter', size_t> counter;
{
std::cout << "Benchmarking T[id] syntax... ";
std::chrono::microseconds seconds1, seconds2;
{
auto t_start = std::chrono::high_resolution_clock::now();
for( size_t i = 0; i < 200000000; ++i )
counter[ obj ]++;
seconds1 = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - t_start);
}
size_t &ref = counter[ obj ];
{
auto t_start = std::chrono::high_resolution_clock::now();
for( size_t i = 0; i < 200000000; ++i )
ref++;
seconds2 = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - t_start);
}
double relative_speed = double( seconds1.count() ) / seconds2.count();
std::cout << "relative access is x" << std::fixed << std::setprecision(2) << relative_speed << " times " << ( seconds1 >= seconds2 ? "slower" : "faster" ) << " than direct access" << std::endl;
}
// debug obj
std::cout << dump(obj) << std::endl;
kult::purge(obj);
// debug obj
std::cout << dump(obj) << std::endl;
}
return 0;
}