Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A benchmark for some elementary functions #3984

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/benchmarks.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ClCompile Include="apsides_benchmark.cpp" />
<ClCompile Include="checkpointer_benchmark.cpp" />
<ClCompile Include="discrete_trajectory_benchmark.cpp" />
<ClCompile Include="elementary_functions_benchmark.cpp" />
<ClCompile Include="lagrange_equipotentials_benchmark.cpp" />
<ClCompile Include="polynomial_in_monomial_basis_benchmark.cpp" />
<ClCompile Include="polynomial_in_чебышёв_basis_benchmark.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions benchmarks/benchmarks.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
<ClCompile Include="polynomial_in_чебышёв_basis_benchmark.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="elementary_functions_benchmark.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="quantities.hpp">
Expand Down
55 changes: 55 additions & 0 deletions benchmarks/elementary_functions_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// .\Release\x64\benchmarks.exe --benchmark_repetitions=3 --benchmark_filter=BM_EvaluateElementaryFunction // NOLINT(whitespace/line_length)

#include <cmath>
#include <random>

#include "benchmark/benchmark.h"

namespace principia {
namespace functions {

static constexpr std::int64_t number_of_iterations = 1000;

enum class Metric { Latency, Throughput };

template<Metric metric, double (__cdecl *fn)(double)>
void BM_EvaluateElementaryFunction(benchmark::State& state) {
using Value = double;
using Argument = double;

std::mt19937_64 random(42);
std::uniform_real_distribution<> uniformly_at(-1.0, 1.0);
Argument argument = uniformly_at(random);

if constexpr (metric == Metric::Throughput) {
Value v[number_of_iterations];
while (state.KeepRunningBatch(number_of_iterations)) {
for (std::int64_t i = 0; i < number_of_iterations; ++i) {
v[i] = fn(argument);
}
benchmark::DoNotOptimize(v);
}
} else {
static_assert(metric == Metric::Latency);
Value v;
while (state.KeepRunningBatch(number_of_iterations)) {
for (std::int64_t i = 0; i < number_of_iterations; ++i) {
v = fn(argument);
argument = v;
}
}
}
}

BENCHMARK_TEMPLATE(BM_EvaluateElementaryFunction, Metric::Latency, std::sin)
->Unit(benchmark::kNanosecond);
BENCHMARK_TEMPLATE(BM_EvaluateElementaryFunction, Metric::Throughput, std::sin)
->Unit(benchmark::kNanosecond);

BENCHMARK_TEMPLATE(BM_EvaluateElementaryFunction, Metric::Latency, std::cos)
->Unit(benchmark::kNanosecond);
BENCHMARK_TEMPLATE(BM_EvaluateElementaryFunction, Metric::Throughput, std::cos)
->Unit(benchmark::kNanosecond);

} // namespace functions
} // namespace principia