Skip to content

Commit 6db73cb

Browse files
committed
Add GCD benchmark
1 parent bebbaa3 commit 6db73cb

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

test/benchmark_u128.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#endif
2323

2424
#include <boost/int128/int128.hpp>
25+
#include <boost/int128/numeric.hpp>
2526
#include <chrono>
2627
#include <random>
2728
#include <vector>
@@ -31,6 +32,11 @@
3132
#include <cmath>
3233
#include <cstring>
3334
#include <functional>
35+
#include <numeric>
36+
37+
#if defined(__cpp_lib_gcd_lcm) && __cpp_lib_gcd_lcm >= 201606L
38+
# define BOOST_INT128_BENCHMARK_BUILTIN_GCD
39+
#endif
3440

3541
constexpr unsigned N = 20'000'000;
3642
constexpr unsigned K = 5;
@@ -311,6 +317,34 @@ BOOST_INT128_NO_INLINE void test_two_element_operation(const std::vector<T>& dat
311317
std::cerr << operation << "<" << std::left << std::setw(11) << type << ">: " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
312318
}
313319

320+
template <typename T>
321+
BOOST_INT128_NO_INLINE void test_gcd(const std::vector<T>& data_vec, const char* type)
322+
{
323+
#ifdef BOOST_INT128_BENCHMARK_BUILTIN_GCD
324+
using std::gcd;
325+
#endif
326+
327+
using boost::multiprecision::gcd;
328+
using boost::int128::gcd;
329+
330+
const auto t1 = std::chrono::steady_clock::now();
331+
std::size_t s = 0; // discard variable
332+
333+
for (std::size_t k {}; k < K; ++k)
334+
{
335+
for (std::size_t i {}; i < data_vec.size() - 1U; ++i)
336+
{
337+
const auto val1 = data_vec[i];
338+
const auto val2 = data_vec[i + 1];
339+
s += static_cast<std::size_t>(gcd(val1, val2));
340+
}
341+
}
342+
343+
const auto t2 = std::chrono::steady_clock::now();
344+
345+
std::cerr << "gcd" << "<" << std::left << std::setw(11) << type << ">: " << std::setw( 10 ) << ( t2 - t1 ) / 1us << " us (s=" << s << ")\n";
346+
}
347+
314348
int main()
315349
{
316350
using namespace boost::int128::detail;
@@ -385,6 +419,15 @@ int main()
385419
test_two_element_operation(mp_vector, std::modulus<>(), "mod", "mp::u128");
386420

387421
std::cerr << std::endl;
422+
423+
#if (defined(BOOST_INT128_HAS_INT128) || defined(BOOST_INT128_HAS_MSVC_INTERNAL_I128)) && defined(BOOST_INT128_BENCHMARK_BUILTIN_GCD)
424+
test_gcd(builtin_vector, "Builtin");
425+
#endif
426+
427+
test_gcd(library_vector, "Library");
428+
test_gcd(mp_vector, "mp::u128");
429+
430+
std::cerr << std::endl;
388431
}
389432
// Single word operations
390433
{

0 commit comments

Comments
 (0)