-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathunformat_benchmark.cpp
140 lines (124 loc) · 3.06 KB
/
unformat_benchmark.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <benchmark/benchmark.h>
#include <regex>
#include <sstream>
#include <cstdio>
#include "unformat.h"
const char* g_input = "Harry is 18 years old and weighs 67.8 kilograms";
const std::string g_inputString = g_input;
#ifdef UNFORMAT_CPP17
#include <string_view>
using string_type = std::string_view;
#else
using string_type = std::string;
#endif
static void Unformat(benchmark::State& state)
{
string_type name;
int age;
float weight;
string_type units;
for (auto _ : state)
{
ay::unformat(g_input, "{} is {} years old and weighs {} {}", name, age, weight, units);
benchmark::DoNotOptimize(name);
benchmark::DoNotOptimize(age);
benchmark::DoNotOptimize(weight);
benchmark::DoNotOptimize(units);
}
}
BENCHMARK(Unformat);
static void Unformat_ConstChar(benchmark::State& state)
{
string_type name;
int age;
float weight;
string_type units;
for (auto _ : state)
{
const char* format = "{} is {} years old and weighs {} {}";
ay::unformat(g_input, format, name, age, weight, units);
benchmark::DoNotOptimize(name);
benchmark::DoNotOptimize(age);
benchmark::DoNotOptimize(weight);
benchmark::DoNotOptimize(units);
}
}
BENCHMARK(Unformat_ConstChar);
static void Unformat_ConstexprMakeFormat(benchmark::State& state)
{
string_type name;
int age;
float weight;
string_type units;
for (auto _ : state)
{
constexpr auto format = ay::make_format("{} is {} years old and weighs {} {}");
ay::unformat(g_input, format, name, age, weight, units);
benchmark::DoNotOptimize(name);
benchmark::DoNotOptimize(age);
benchmark::DoNotOptimize(weight);
benchmark::DoNotOptimize(units);
}
}
BENCHMARK(Unformat_ConstexprMakeFormat);
static void StdStringStream(benchmark::State& state)
{
std::string name;
int age;
float weight;
std::string units;
for (auto _ : state)
{
std::istringstream stream(g_input);
stream >> name;
stream.ignore(2);
stream >> age;
stream.ignore(21);
stream >> weight;
stream >> units;
benchmark::DoNotOptimize(name);
benchmark::DoNotOptimize(age);
benchmark::DoNotOptimize(weight);
benchmark::DoNotOptimize(units);
}
}
BENCHMARK(StdStringStream);
static void StdRegex(benchmark::State& state)
{
std::string name;
int age;
float weight;
std::string units;
for (auto _ : state)
{
std::regex regex("([A-Za-z]+) is ([0-9]+) years old and weighs ([0-9\\.]+) ([A-Za-z]+)");
std::smatch matches;
std::regex_search(g_inputString, matches, regex);
name = matches[1];
age = std::stoi(matches[2]);
weight = std::stof(matches[3]);
units = matches[4];
benchmark::DoNotOptimize(name);
benchmark::DoNotOptimize(age);
benchmark::DoNotOptimize(weight);
benchmark::DoNotOptimize(units);
}
}
BENCHMARK(StdRegex);
static void StdScanf(benchmark::State& state)
{
char name[16];
int age;
float weight;
char units[16];
for (auto _ : state)
{
std::sscanf(g_input, "%s is %d years old and weighs %f %s", &name, &age, &weight, &units);
benchmark::DoNotOptimize(name);
benchmark::DoNotOptimize(age);
benchmark::DoNotOptimize(weight);
benchmark::DoNotOptimize(units);
}
}
BENCHMARK(StdScanf);
BENCHMARK_MAIN();