Skip to content

Commit 27a3cc0

Browse files
UdjinM6CryptoCentric
authored andcommitted
Add string_cast benchmark (dashpay#2073)
* Add string_cast benchmark * add NumberToString
1 parent 04972d6 commit 27a3cc0

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

src/Makefile.bench.include

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ bench_bench_absolute_SOURCES = \
2424
bench/base58.cpp \
2525
bench/lockedpool.cpp \
2626
bench/perf.cpp \
27-
bench/perf.h
27+
bench/perf.h \
28+
bench/string_cast.cpp
2829

2930
nodist_bench_bench_absolute_SOURCES = $(GENERATED_TEST_FILES)
3031

src/bench/string_cast.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (c) 2018 The Dash Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "bench.h"
6+
#include "tinyformat.h"
7+
#include "utilstrencodings.h"
8+
9+
#include <boost/lexical_cast.hpp>
10+
#include <string>
11+
12+
template <typename T>
13+
std::string NumberToString(T Number){
14+
std::ostringstream oss;
15+
oss << Number;
16+
return oss.str();
17+
}
18+
19+
static void int_atoi(benchmark::State& state)
20+
{
21+
while (state.KeepRunning())
22+
atoi("1");
23+
}
24+
25+
static void int_lexical_cast(benchmark::State& state)
26+
{
27+
while (state.KeepRunning())
28+
boost::lexical_cast<int>("1");
29+
}
30+
31+
static void strings_1_itostr(benchmark::State& state)
32+
{
33+
int i{0};
34+
while (state.KeepRunning())
35+
itostr(++i);
36+
}
37+
38+
static void strings_1_lexical_cast(benchmark::State& state)
39+
{
40+
int i{0};
41+
while (state.KeepRunning())
42+
boost::lexical_cast<std::string>(++i);
43+
}
44+
45+
static void strings_1_numberToString(benchmark::State& state)
46+
{
47+
int i{0};
48+
while (state.KeepRunning())
49+
NumberToString(++i);
50+
}
51+
52+
static void strings_1_to_string(benchmark::State& state)
53+
{
54+
int i{0};
55+
while (state.KeepRunning())
56+
std::to_string(++i);
57+
}
58+
59+
static void strings_2_multi_itostr(benchmark::State& state)
60+
{
61+
int i{0};
62+
while (state.KeepRunning()) {
63+
itostr(i) + itostr(i+1) + itostr(i+2) + itostr(i+3) + itostr(i+4);
64+
++i;
65+
}
66+
}
67+
68+
static void strings_2_multi_lexical_cast(benchmark::State& state)
69+
{
70+
int i{0};
71+
while (state.KeepRunning()) {
72+
boost::lexical_cast<std::string>(i) +
73+
boost::lexical_cast<std::string>(i+1) +
74+
boost::lexical_cast<std::string>(i+2) +
75+
boost::lexical_cast<std::string>(i+3) +
76+
boost::lexical_cast<std::string>(i+4);
77+
++i;
78+
}
79+
}
80+
81+
static void strings_2_multi_numberToString(benchmark::State& state)
82+
{
83+
int i{0};
84+
while (state.KeepRunning()) {
85+
NumberToString(i) + NumberToString(i+1) + NumberToString(i+2) + NumberToString(i+3) + NumberToString(i+4);
86+
++i;
87+
}
88+
}
89+
90+
static void strings_2_multi_to_string(benchmark::State& state)
91+
{
92+
int i{0};
93+
while (state.KeepRunning()) {
94+
std::to_string(i) + std::to_string(i+1) + std::to_string(i+2) + std::to_string(i+3) + std::to_string(i+4);
95+
++i;
96+
}
97+
}
98+
99+
static void strings_2_strptintf(benchmark::State& state)
100+
{
101+
int i{0};
102+
while (state.KeepRunning()) {
103+
strprintf("%d|%d|%d|%d|%d", i, i+1, i+2, i+3, i+4);
104+
++i;
105+
}
106+
}
107+
108+
BENCHMARK(int_atoi);
109+
BENCHMARK(int_lexical_cast);
110+
BENCHMARK(strings_1_itostr);
111+
BENCHMARK(strings_1_lexical_cast);
112+
BENCHMARK(strings_1_numberToString);
113+
BENCHMARK(strings_1_to_string);
114+
BENCHMARK(strings_2_multi_itostr);
115+
BENCHMARK(strings_2_multi_lexical_cast);
116+
BENCHMARK(strings_2_multi_numberToString);
117+
BENCHMARK(strings_2_multi_to_string);
118+
BENCHMARK(strings_2_strptintf);

0 commit comments

Comments
 (0)