This repository was archived by the owner on Sep 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.cpp
95 lines (86 loc) · 2.5 KB
/
performance.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
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
// cpp-flexargs
//
// Copyright iorate 2018.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Reference: http://www.kmonos.net/alang/boost/classes/program_options.html
#include <iostream>
#include <stdexcept>
#include <string_view>
#include <utility>
#include <boost/timer/timer.hpp>
#include "../flexargs.hpp"
using namespace flexargs;
int calc_v0(std::string_view op, int lhs = 100, int rhs = 200) {
if (op == "add") {
return lhs + rhs;
} else if (op == "sub") {
return lhs - rhs;
} else if (op == "mul") {
return lhs * rhs;
} else if (op == "div") {
if (rhs == 0) {
throw std::invalid_argument("division by zero");
} else {
return lhs / rhs;
}
} else {
throw std::invalid_argument("bad operator");
}
}
namespace keywords {
inline constexpr keyword<struct op_> op;
inline constexpr keyword<struct lhs_> lhs;
inline constexpr keyword<struct rhs_> rhs;
}
template <class ...Args>
int calc_v1(Args &&...args) {
auto [op, lhs, rhs] = match(
parameter<std::string_view>(keywords::op),
parameter<int>(keywords::lhs) = 100,
parameter<int>(keywords::rhs) = 200,
std::forward<Args>(args)...
);
if (op == "add") {
return lhs + rhs;
} else if (op == "sub") {
return lhs - rhs;
} else if (op == "mul") {
return lhs * rhs;
} else if (op == "div") {
if (rhs == 0) {
throw std::invalid_argument("division by zero");
} else {
return lhs / rhs;
}
} else {
throw std::invalid_argument("bad operator");
}
}
int main() {
using namespace keywords;
constexpr int N = 100'000'000;
{
std::cout << "call calc_v0() " << N << " times:\n";
boost::timer::auto_cpu_timer timer;
for (int i = 0; i < N; ++i) {
calc_v0("sub", 999);
}
}
{
std::cout << "call calc_v1() " << N << " times:\n";
boost::timer::auto_cpu_timer timer;
for (int i = 0; i < N; ++i) {
calc_v1(op = "sub", lhs = 999);
}
}
}
/*
$ g++ -std=c++17 -O2 performance.cpp -lboost_timer -o performance
$ ./performance
call calc_v0() 100000000 times:
0.284747s wall, 0.296875s user + 0.000000s system = 0.296875s CPU (104.3%)
call calc_v1() 100000000 times:
0.729618s wall, 0.718750s user + 0.000000s system = 0.718750s CPU (98.5%)
*/