Skip to content

Commit 89b9f26

Browse files
committed
Remove redundencies with algo filtering
1 parent 35da23f commit 89b9f26

File tree

4 files changed

+46
-63
lines changed

4 files changed

+46
-63
lines changed

benchmarks/benchmark.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,22 @@
2727

2828
using Benchmarks::BenchArgs;
2929

30-
bool is_matched(const std::string &str, const std::span<std::string> filter) {
31-
if (filter.empty()) {
32-
return true;
33-
}
34-
for (const auto &f : filter) {
35-
if (str.find(f) != std::string::npos) {
36-
return true;
37-
}
38-
}
39-
return false;
40-
}
41-
4230
template <arithmetic_float T>
4331
void evaluateProperties(const std::vector<T> &lines,
4432
const std::array<BenchArgs<T>, Benchmarks::COUNT> &args,
45-
const std::span<std::string> filter = {}) {
33+
const std::vector<std::string> &algo_filter) {
4634
fmt::println("{:20} {:20}", "Algorithm", "Valid round-trip");
4735

4836
for (const auto &algo : args) {
4937
if (!algo.used) {
5038
fmt::println("# skipping {}", algo.name);
5139
continue;
5240
}
53-
// Apply filter if provided
54-
if (!is_matched(algo.name, filter)) {
41+
if (algo_filtered_out(algo.name, algo_filter)) {
5542
fmt::println("# filtered out {}", algo.name);
5643
continue;
5744
}
45+
5846
char buf1[100], buf2[100];
5947
std::span<char> bufRef(buf1, sizeof(buf1)), bufAlgo(buf2, sizeof(buf2));
6048
int incorrect = 0;
@@ -94,10 +82,11 @@ struct diy_float_t {
9482

9583
template <arithmetic_float T>
9684
void process(const std::vector<T> &lines,
97-
const std::array<BenchArgs<T>, Benchmarks::COUNT> &args, const std::span<std::string> filter = {}) {
85+
const std::array<BenchArgs<T>, Benchmarks::COUNT> &args,
86+
const std::vector<std::string> &algo_filter) {
9887
// We have a special algorithm for the string generation:
99-
std::string just_string = "just_string";
100-
if (is_matched(just_string, filter)) {
88+
if (const std::string just_string = "just_string";
89+
!algo_filtered_out(just_string, algo_filter)) {
10190
std::vector<diy_float_t> parsed;
10291
for(auto d : lines) {
10392
auto v = jkj::grisu_exact(d);
@@ -114,16 +103,17 @@ void process(const std::vector<T> &lines,
114103
} else {
115104
fmt::println("# skipping {}", just_string);
116105
}
106+
117107
for (const auto &algo : args) {
118108
if (!algo.used) {
119109
fmt::println("# skipping {}", algo.name);
120110
continue;
121111
}
122-
// Apply filter if provided
123-
if (!is_matched(algo.name, filter)) {
112+
if (algo_filtered_out(algo.name, algo_filter)) {
124113
fmt::println("# filtered out {}", algo.name);
125114
continue;
126115
}
116+
127117
pretty_print(lines, algo.name, [&algo](const std::vector<T> &lines) -> int {
128118
int volume = 0;
129119
char buf[100];
@@ -133,14 +123,13 @@ void process(const std::vector<T> &lines,
133123
return volume;
134124
}, algo.testRepeat);
135125
}
136-
137126
}
138127

139128
template <typename T>
140129
std::vector<T> fileload(const std::string &filename) {
141130
std::ifstream inputfile(filename);
142131
if (!inputfile) {
143-
fmt::print(stderr, "can't open {}\n", filename);
132+
fmt::println(stderr, "can't open {}", filename);
144133
return {};
145134
}
146135

@@ -151,7 +140,7 @@ std::vector<T> fileload(const std::string &filename) {
151140
lines.push_back(std::is_same_v<T, float> ? std::stof(line)
152141
: std::stod(line));
153142
} catch (...) {
154-
fmt::print(stderr, "problem with {}\nWe expect floating-point numbers (one per line).\n", line);
143+
fmt::println(stderr, "problem with {}\nWe expect floating-point numbers (one per line).", line);
155144
std::abort();
156145
}
157146
}
@@ -165,7 +154,7 @@ std::vector<T> get_random_numbers(size_t howmany,
165154
fmt::println("# parsing random numbers");
166155
std::vector<T> lines;
167156
auto g = get_generator_by_name<T>(random_model);
168-
fmt::print("model: {}\nvolume: {} floats\n", g->describe(), howmany);
157+
fmt::println("model: {}\nvolume: {} floats", g->describe(), howmany);
169158
lines.reserve(howmany); // let us reserve plenty of memory.
170159
for (size_t i = 0; i < howmany; i++) {
171160
const T line = g->new_float();
@@ -218,7 +207,8 @@ int main(int argc, char **argv) {
218207
numbers = get_random_numbers<float>(volume, model);
219208
else
220209
numbers = get_random_numbers<double>(volume, model);
221-
fmt::println("# You can also provide a filename (with the -f flag): it should contain one string per line corresponding to a number");
210+
fmt::println("# You can also provide a filename (with the -f flag):"
211+
"it should contain one string per line corresponding to a number");
222212
}
223213
else {
224214
if (single)

benchmarks/benchutil.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
#ifndef BENCHUTIL_H
22
#define BENCHUTIL_H
33

4-
#include "counters/event_counter.h"
4+
#include <atomic>
55
#include <cfloat>
66
#include <cstdio>
77

8-
#include <atomic>
8+
#include "counters/event_counter.h"
9+
910
event_collector collector;
1011

12+
bool algo_filtered_out(const std::string &algo_name,
13+
const std::vector<std::string> &algo_filter) {
14+
if (algo_filter.empty())
15+
return false;
16+
17+
for (const auto &f : algo_filter)
18+
if (algo_name.find(f) != std::string::npos)
19+
return false;
20+
21+
return true;
22+
}
23+
1124
template <class function_type>
1225
event_aggregate bench(const function_type &&function, size_t min_repeat = 10,
1326
size_t min_time_ns = 400'000'000,

benchmarks/exhaustivefloat32.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "algorithms.h"
1414
#include "cxxopts.hpp"
1515
#include "floatutils.h"
16+
#include "benchutil.h"
1617

1718
void run_exhaustive32(bool errol, const std::vector<std::string>& algo_filter = {}) {
1819
fmt::println("{:20} {:20}", "Algorithm", "Valid shortest serialization");
@@ -22,27 +23,16 @@ void run_exhaustive32(bool errol, const std::vector<std::string>& algo_filter =
2223

2324
for (const auto &algo : args) {
2425
if (!algo.used) {
25-
fmt::print("# skipping {}\n", algo.name);
26+
fmt::println("# skipping {}", algo.name);
2627
continue;
2728
}
2829
if (algo.func == Benchmarks::dragonbox<float>) {
29-
fmt::print("# skipping {} because it is the reference.\n", algo.name);
30+
fmt::println("# skipping {} because it is the reference.", algo.name);
3031
continue;
3132
}
32-
33-
// Apply filter if provided
34-
if (!algo_filter.empty()) {
35-
bool matched = false;
36-
for (const auto &f : algo_filter) {
37-
if (algo.name.find(f) != std::string::npos) {
38-
matched = true;
39-
break;
40-
}
41-
}
42-
if (!matched) {
43-
fmt::print("# filtered out {}\n", algo.name);
44-
continue;
45-
}
33+
if (algo_filtered_out(algo.name, algo_filter)) {
34+
fmt::println("# filtered out {}", algo.name);
35+
continue;
4636
}
4737

4838
bool incorrect = false;
@@ -131,14 +121,14 @@ int main(int argc, char **argv) {
131121
const auto result = options.parse(argc, argv);
132122

133123
if (result["help"].as<bool>()) {
134-
fmt::print("{}\n", options.help());
124+
fmt::println("{}", options.help());
135125
return EXIT_SUCCESS;
136126
}
137127

138128
auto algo_filter = result["algorithm"].as<std::vector<std::string>>();
139129
run_exhaustive32(result["errol"].as<bool>(), algo_filter);
140130
} catch (const std::exception &e) {
141-
fmt::print("error parsing options: {}\n", e.what());
131+
fmt::println("error parsing options: {}", e.what());
142132
return EXIT_FAILURE;
143133
}
144134
}

benchmarks/thoroughfloat64.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "algorithms.h"
1616
#include "cxxopts.hpp"
1717
#include "floatutils.h"
18+
#include "benchutil.h"
1819

1920
struct test_case {
2021
double value;
@@ -57,27 +58,16 @@ void run_file_test(const std::string& filename, bool errol, const std::vector<st
5758

5859
for (const auto &algo : args) {
5960
if (!algo.used) {
60-
fmt::print("# skipping {}\n", algo.name);
61+
fmt::println("# skipping {}", algo.name);
6162
continue;
6263
}
6364
if (algo.func == Benchmarks::dragonbox<double>) {
64-
fmt::print("# skipping {} because it is the reference.\n", algo.name);
65+
fmt::println("# skipping {} because it is the reference.", algo.name);
6566
continue;
6667
}
67-
68-
// Apply filter if provided
69-
if (!algo_filter.empty()) {
70-
bool matched = false;
71-
for (const auto &f : algo_filter) {
72-
if (algo.name.find(f) != std::string::npos) {
73-
matched = true;
74-
break;
75-
}
76-
}
77-
if (!matched) {
78-
fmt::print("# filtered out {}\n", algo.name);
79-
continue;
80-
}
68+
if (algo_filtered_out(algo.name, algo_filter)) {
69+
fmt::println("# filtered out {}", algo.name);
70+
continue;
8171
}
8272

8373
bool incorrect = false;
@@ -168,12 +158,12 @@ int main(int argc, char **argv) {
168158
const auto result = options.parse(argc, argv);
169159

170160
if (result["help"].as<bool>()) {
171-
fmt::print("{}\n", options.help());
161+
fmt::println("{}", options.help());
172162
return EXIT_SUCCESS;
173163
}
174164
run_file_test(result["file"].as<std::string>(), result["errol"].as<bool>(), result["algorithm"].as<std::vector<std::string>>());
175165
} catch (const std::exception &e) {
176-
fmt::print("error parsing options: {}\n", e.what());
166+
fmt::println("error parsing options: {}", e.what());
177167
return EXIT_FAILURE;
178168
}
179169
}

0 commit comments

Comments
 (0)