Skip to content

Commit 06bd7fc

Browse files
author
Daniel Lemire
committed
some tuning and more options
1 parent ff56c15 commit 06bd7fc

File tree

6 files changed

+212
-81
lines changed

6 files changed

+212
-81
lines changed

benchmarks/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ add_executable(benchmark
33
)
44
add_library(benchmark_deps INTERFACE)
55
add_library(ieeeToString ieeeToString.cpp)
6-
target_include_directories(ieeeToString PRIVATE ${ryu_SOURCE_DIR})
76
target_link_libraries(benchmark_deps INTERFACE ieeeToString)
87
include(CheckSourceCompiles)
98
check_source_compiles(CXX "

benchmarks/algorithms.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct BenchArgs {
8282
std::string name{};
8383
int (*func)(T, std::span<char>&){};
8484
bool used{};
85-
unsigned char testRepeat{100};
85+
size_t testRepeat{100};
8686
};
8787

8888
template<arithmetic_float T>

benchmarks/benchmark.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,21 @@
2626
using Benchmarks::arithmetic_float;
2727
using Benchmarks::BenchArgs;
2828

29+
bool is_matched(const std::string &str, const std::span<std::string> filter) {
30+
if (filter.empty()) {
31+
return true;
32+
}
33+
for (const auto &f : filter) {
34+
if (str.find(f) != std::string::npos) {
35+
return true;
36+
}
37+
}
38+
return false;
39+
}
40+
2941
template <arithmetic_float T>
3042
void evaluateProperties(const std::vector<T> &lines,
31-
const std::array<BenchArgs<T>, Benchmarks::COUNT> &args, const std::string& filter = "") {
43+
const std::array<BenchArgs<T>, Benchmarks::COUNT> &args, const std::span<std::string> filter = {}) {
3244
constexpr auto precision = std::numeric_limits<T>::digits10;
3345
fmt::println("{:20} {:20}", "Algorithm", "Valid round-trip");
3446

@@ -38,7 +50,7 @@ void evaluateProperties(const std::vector<T> &lines,
3850
continue;
3951
}
4052
// Apply filter if provided
41-
if (!filter.empty() && std::string(filter).find(algo.name) == std::string::npos) {
53+
if (!is_matched(algo.name, filter)) {
4254
std::cout << "# filtered out " << algo.name << std::endl;
4355
continue;
4456
}
@@ -73,14 +85,14 @@ void evaluateProperties(const std::vector<T> &lines,
7385

7486
template <arithmetic_float T>
7587
void process(const std::vector<T> &lines,
76-
const std::array<BenchArgs<T>, Benchmarks::COUNT> &args, const std::string& filter = "") {
88+
const std::array<BenchArgs<T>, Benchmarks::COUNT> &args, const std::span<std::string> filter = {}) {
7789
for (const auto &algo : args) {
7890
if (!algo.used) {
7991
std::cout << "# skipping " << algo.name << std::endl;
8092
continue;
8193
}
8294
// Apply filter if provided
83-
if (!filter.empty() && std::string(filter).find(algo.name) == std::string::npos) {
95+
if (!is_matched(algo.name, filter)) {
8496
std::cout << "# filtered out " << algo.name << std::endl;
8597
continue;
8698
}
@@ -155,18 +167,20 @@ int main(int argc, char **argv) {
155167
cxxopts::value<bool>()->default_value("false"))
156168
("e,errol", "Enable errol3 (current impl. returns invalid values, e.g., for 0).",
157169
cxxopts::value<bool>()->default_value("false"))
158-
("a,algo-filter", "Filter algorithms by name substring.",
159-
cxxopts::value<std::string>()->default_value(""))
170+
("a,algo-filter", "Filter algorithms by name substring: you can use multiple filters separated by commas.",
171+
cxxopts::value<std::vector<std::string>>()->default_value(""))
172+
("r,repeat", "Force a number of repetitions.",
173+
cxxopts::value<size_t>()->default_value("0"))
160174
("h,help", "Print usage.");
161175
const auto result = options.parse(argc, argv);
162176

163177
if (result["help"].as<bool>()) {
164178
std::cout << options.help() << std::endl;
165179
return EXIT_SUCCESS;
166180
}
167-
181+
const size_t repeat = result["repeat"].as<size_t>();
168182
const bool single = result["single"].as<bool>();
169-
const std::string filter = result["algo-filter"].as<std::string>();
183+
std::vector<std::string> filter = result["algo-filter"].as<std::vector<std::string>>();
170184
std::cout << "number type: binary"
171185
<< (single ? "32 (float)" : "64 (double)") << std::endl;
172186

@@ -198,6 +212,14 @@ int main(int argc, char **argv) {
198212
else
199213
algorithms = Benchmarks::initArgs<double>(errol);
200214

215+
if(repeat > 0) {
216+
std::cout << "# forcing repeat count to " << repeat << std::endl;
217+
std::visit([repeat](auto &args) {
218+
for (auto &arg : args)
219+
arg.testRepeat = repeat;
220+
}, algorithms);
221+
}
222+
201223
const bool test = result["test"].as<bool>();
202224
std::visit([test,&filter](const auto &lines, const auto &args) {
203225
using T1 = typename std::decay_t<decltype(lines)>::value_type;

benchmarks/exhaustivefloat32.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <iostream>
99
#include <string_view>
1010
#include <charconv>
11+
#include <vector>
1112

1213
#include "algorithms.h"
1314
#include "cxxopts.hpp"
@@ -59,7 +60,7 @@ std::optional<float> parse_float(std::string_view sv) {
5960
return std::nullopt;
6061
}
6162

62-
void run_exhaustive32(bool errol) {
63+
void run_exhaustive32(bool errol, const std::vector<std::string>& algo_filter = {}) {
6364
constexpr auto precision = std::numeric_limits<float>::digits10;
6465
fmt::println("{:20} {:20}", "Algorithm", "Valid shortest serialization");
6566

@@ -75,6 +76,22 @@ void run_exhaustive32(bool errol) {
7576
fmt::print("# skipping {} because it is the reference.\n", algo.name);
7677
continue;
7778
}
79+
80+
// Apply filter if provided
81+
if (!algo_filter.empty()) {
82+
bool matched = false;
83+
for (const auto &f : algo_filter) {
84+
if (algo.name.find(f) != std::string::npos) {
85+
matched = true;
86+
break;
87+
}
88+
}
89+
if (!matched) {
90+
fmt::print("# filtered out {}\n", algo.name);
91+
continue;
92+
}
93+
}
94+
7895
bool incorrect = false;
7996
char buf1[100], buf2[100];
8097
std::span<char> bufRef(buf1, sizeof(buf1)), bufAlgo(buf2, sizeof(buf2));
@@ -149,15 +166,21 @@ int main(int argc, char **argv) {
149166
options.add_options()(
150167
"e,errol",
151168
"Enable errol3 (current impl. returns invalid values, e.g., for 0).",
152-
cxxopts::value<bool>()->default_value("false"))("h,help",
153-
"Print usage.");
169+
cxxopts::value<bool>()->default_value("false"))(
170+
"a,algorithm",
171+
"Specify which algorithm(s) to test (comma-separated).",
172+
cxxopts::value<std::vector<std::string>>()->default_value({}))(
173+
"h,help",
174+
"Print usage.");
154175
const auto result = options.parse(argc, argv);
155176

156177
if (result["help"].as<bool>()) {
157178
fmt::print("{}\n", options.help());
158179
return EXIT_SUCCESS;
159180
}
160-
run_exhaustive32(result["errol"].as<bool>());
181+
182+
auto algo_filter = result["algorithm"].as<std::vector<std::string>>();
183+
run_exhaustive32(result["errol"].as<bool>(), algo_filter);
161184
} catch (const std::exception &e) {
162185
fmt::print("error parsing options: {}\n", e.what());
163186
return EXIT_FAILURE;

0 commit comments

Comments
 (0)