-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
70 lines (60 loc) · 2.02 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
67
68
69
70
#include <chrono>
#include <exception>
#include <iostream>
using namespace std;
static uint32_t checkPoint = 100;
static const uint32_t testCount = 1000000000l;
uint64_t toInt(uint64_t value, bool *ok) noexcept
{
bool valid = true;
if (value % checkPoint == 0)
valid = false;
if (ok)
*ok = valid;
if (!valid)
return value;
return ++value;
}
uint64_t toInt(uint64_t value)
{
if (value % checkPoint == 0)
throw std::invalid_argument("bla bla");
return ++value;
}
int main()
{
cout << "Benchmarking exceptions, doing " << testCount << " function calls" << endl;
while (checkPoint < testCount) {
cout << "Throw an error every " << checkPoint << " calls" << endl;
auto startError = chrono::high_resolution_clock::now();
for (uint64_t test = 0; test < testCount;) {
bool ok;
test = toInt(test, &ok);
if (!ok)
++test;
}
auto stopError = chrono::high_resolution_clock::now();
auto startThrow = chrono::high_resolution_clock::now();
for (uint64_t test = 0; test < testCount;) {
try {
test = toInt(test);
} catch (...) {
++test;
}
}
auto stopThrow = chrono::high_resolution_clock::now();
auto errorTicks = (stopError - startError).count();
auto throwTicks = (stopThrow - startThrow).count();
cout << "Error ticks " << errorTicks << endl << "Throw ticks " << throwTicks << endl;
if (errorTicks > throwTicks) {
auto ratio = double(errorTicks)/double(throwTicks) ;
cout << "Throw is x" << ratio << " times (" << (ratio -1) * 100 << "%) faster" << endl;
} else {
auto ratio = double(throwTicks)/double(errorTicks);
cout << "Error is x" << ratio << " times (" << (ratio -1) * 100 << "%) faster" << endl;
}
cout << "-------------------------------" << endl;
checkPoint *= 10;
}
return 0;
}