Skip to content
This repository was archived by the owner on Sep 9, 2021. It is now read-only.

Commit 96a56c9

Browse files
committed
Update the performance example
1 parent 75013a4 commit 96a56c9

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ performance.cpp
196196
#include "../flexargs.hpp"
197197
using namespace flexargs;
198198

199-
int calc1(std::string_view op, int lhs = 100, int rhs = 200) {
199+
int calc_v0(std::string_view op, int lhs = 100, int rhs = 200) {
200200
if (op == "add") {
201201
return lhs + rhs;
202202
} else if (op == "sub") {
@@ -221,7 +221,7 @@ namespace keywords {
221221
}
222222

223223
template <class ...Args>
224-
int calc2(Args &&...args) {
224+
int calc_v1(Args &&...args) {
225225
auto [op, lhs, rhs] = match(
226226
parameter<std::string_view>(keywords::op),
227227
parameter<int>(keywords::lhs) = 100,
@@ -248,17 +248,17 @@ int main() {
248248
using namespace keywords;
249249
constexpr int N = 100000000;
250250
{
251-
std::cout << "call calc1() " << N << " times:\n";
251+
std::cout << "call calc_v0() " << N << " times:\n";
252252
boost::timer::auto_cpu_timer timer;
253253
for (int i = 0; i < N; ++i) {
254-
calc1("sub", 999);
254+
calc_v0("sub", 999);
255255
}
256256
}
257257
{
258-
std::cout << "call calc2() " << N << " times:\n";
258+
std::cout << "call calc_v1() " << N << " times:\n";
259259
boost::timer::auto_cpu_timer timer;
260260
for (int i = 0; i < N; ++i) {
261-
calc2(op = "sub", lhs = 999);
261+
calc_v1(op = "sub", lhs = 999);
262262
}
263263
}
264264
}
@@ -267,9 +267,9 @@ int main() {
267267
$ g++ -std=c++17 -O2 performance.cpp -lboost_timer -o performance
268268

269269
$ ./performance
270-
call calc1() 100000000 times:
270+
call calc_v0() 100000000 times:
271271
0.284747s wall, 0.296875s user + 0.000000s system = 0.296875s CPU (104.3%)
272-
call calc2() 100000000 times:
272+
call calc_v1() 100000000 times:
273273
0.729618s wall, 0.718750s user + 0.000000s system = 0.718750s CPU (98.5%)
274274
```
275275
When I compiled it by GCC 8.2.0 with '-O2' flag and executed it in my environment, the extra execution time was approximately 0.45s. That means the overhead per call was 4.5ns, which is probably an acceptable value in most cases.

example/performance.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "../flexargs.hpp"
1717
using namespace flexargs;
1818

19-
int calc1(std::string_view op, int lhs = 100, int rhs = 200) {
19+
int calc_v0(std::string_view op, int lhs = 100, int rhs = 200) {
2020
if (op == "add") {
2121
return lhs + rhs;
2222
} else if (op == "sub") {
@@ -41,7 +41,7 @@ namespace keywords {
4141
}
4242

4343
template <class ...Args>
44-
int calc2(Args &&...args) {
44+
int calc_v1(Args &&...args) {
4545
auto [op, lhs, rhs] = match(
4646
parameter<std::string_view>(keywords::op),
4747
parameter<int>(keywords::lhs) = 100,
@@ -68,17 +68,17 @@ int main() {
6868
using namespace keywords;
6969
constexpr int N = 100000000;
7070
{
71-
std::cout << "call calc1() " << N << " times:\n";
71+
std::cout << "call calc_v0() " << N << " times:\n";
7272
boost::timer::auto_cpu_timer timer;
7373
for (int i = 0; i < N; ++i) {
74-
calc1("sub", 999);
74+
calc_v0("sub", 999);
7575
}
7676
}
7777
{
78-
std::cout << "call calc2() " << N << " times:\n";
78+
std::cout << "call calc_v1() " << N << " times:\n";
7979
boost::timer::auto_cpu_timer timer;
8080
for (int i = 0; i < N; ++i) {
81-
calc2(op = "sub", lhs = 999);
81+
calc_v1(op = "sub", lhs = 999);
8282
}
8383
}
8484
}
@@ -87,8 +87,8 @@ int main() {
8787
$ g++ -std=c++17 -O2 performance.cpp -lboost_timer -o performance
8888
8989
$ ./performance
90-
call calc1() 100000000 times:
90+
call calc_v0() 100000000 times:
9191
0.284747s wall, 0.296875s user + 0.000000s system = 0.296875s CPU (104.3%)
92-
call calc2() 100000000 times:
92+
call calc_v1() 100000000 times:
9393
0.729618s wall, 0.718750s user + 0.000000s system = 0.718750s CPU (98.5%)
9494
*/

0 commit comments

Comments
 (0)