Skip to content

Commit 85c90ec

Browse files
committed
Add std::format to string formatting benchmarks
Currently only MSVC supports std::format, so the documented benchmarks are not updated (they are generated with GCC on Linux).
1 parent 0757d32 commit 85c90ec

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

bench/string/README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# JSON Parser
1+
# String formatting
22

3-
Benchmark of the libfly JSON parser against the popular {fmt} library and the STL:
3+
Benchmark of the libfly string formatter against the popular {fmt} library and the STL:
44

55
* [libfly](/fly/types/string/string.hpp)
66
* [{fmt}](https://github.com/fmtlib/fmt)
@@ -11,19 +11,19 @@ All results below are the median of 1000001 iterations of creating a formatted s
1111

1212
### Formatting with floats
1313

14-
| Formatter | Duration (ns) |
15-
| :-- | --: |
16-
| libfly | 1.093 |
17-
| {fmt} | 0.609 |
18-
| STL IO Streams | 1.812 |
14+
| Formatter | Duration (ns) |
15+
| :-- | --: |
16+
| libfly | 1.093 |
17+
| {fmt} | 0.609 |
18+
| std::stringstream | 1.812 |
1919

2020
### Formatting without floats
2121

22-
| Formatter | Duration (ns) |
23-
| :-- | --: |
24-
| libfly | 0.665 |
25-
| {fmt} | 0.411 |
26-
| STL IO Streams | 1.011 |
22+
| Formatter | Duration (ns) |
23+
| :-- | --: |
24+
| libfly | 0.665 |
25+
| {fmt} | 0.411 |
26+
| std::stringstream | 1.011 |
2727

2828
## Profiles
2929

bench/string/benchmark_string.cpp

+30-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#include <sstream>
1111
#include <string>
1212

13+
#if __has_include(<format>)
14+
# include <format>
15+
#endif
16+
1317
namespace {
1418

1519
using StringTable = fly::benchmark::Table<std::string, double>;
@@ -69,8 +73,29 @@ class FmtFormat : public StringBase
6973
}
7074
};
7175

76+
#if __has_include(<format>)
77+
78+
// std::format
79+
class StdFormat : public StringBase
80+
{
81+
public:
82+
void format_with_floats() override
83+
{
84+
FLY_UNUSED(
85+
std::format("{:.10f}:{:04}:{:+}:{}:{}:{}:%\n", 1.234, 42, 3.13, "str", nullptr, 'X'));
86+
}
87+
88+
void format_without_floats() override
89+
{
90+
FLY_UNUSED(
91+
std::format("{:10}:{:04}:{:+}:{}:{}:{}:%\n", 1234, 42, 313, "str", nullptr, 'X'));
92+
}
93+
};
94+
95+
#endif
96+
7297
// STL IO Streams
73-
class STLStreamFormat : public StringBase
98+
class StreamFormat : public StringBase
7499
{
75100
public:
76101
void format_with_floats() override
@@ -99,7 +124,10 @@ void run_format_test(std::string &&name)
99124
std::map<std::string, std::unique_ptr<StringBase>> formatters;
100125
#if !defined(FLY_PROFILE)
101126
formatters.emplace("{fmt}", std::make_unique<FmtFormat>());
102-
formatters.emplace("STL IO Streams", std::make_unique<STLStreamFormat>());
127+
formatters.emplace("std::stringstream", std::make_unique<StreamFormat>());
128+
# if __has_include(<format>)
129+
formatters.emplace("std::format", std::make_unique<StdFormat>());
130+
# endif
103131
#endif
104132
formatters.emplace("libfly", std::make_unique<LibflyFormat>());
105133

0 commit comments

Comments
 (0)