-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmul_benchmark.cpp
More file actions
81 lines (63 loc) · 2.14 KB
/
Copy pathmul_benchmark.cpp
File metadata and controls
81 lines (63 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <benchmark/benchmark.h>
#include <random>
#define main main2
#include "mul.cpp"
#undef main
using namespace std;
static random_device rd;
static mt19937 rng{rd()};
static uniform_int_distribution<> digit_distrib('0', '9');
static void generate_random_digits(string &s) {
for (char &c : s)
c = static_cast<char>(digit_distrib(rng));
if (rng() & 1)
s[0] = '-';
}
static void BM_BigIntegerParsing(benchmark::State &state) {
string x(state.range(0), '0');
for (auto _ : state) {
state.PauseTiming();
generate_random_digits(x);
state.ResumeTiming();
BigInteger integer(x);
benchmark::DoNotOptimize(integer);
benchmark::ClobberMemory();
}
state.SetComplexityN(state.range(0));
}
BENCHMARK(BM_BigIntegerParsing)
->RangeMultiplier(10)->Range(10, 10000000)->Complexity(benchmark::oN);
static void BM_BigDecimalParsing(benchmark::State &state) {
const int64_t exponent_maximum = state.range(0) * state.range(0);
uniform_int_distribution<int64_t> exponent_distrib(-exponent_maximum, exponent_maximum);
string x(state.range(0), '0');
for (auto _ : state) {
state.PauseTiming();
generate_random_digits(x);
x[rng() % x.length()] = '.';
string y = x + 'e' + to_string(exponent_distrib(rng));
state.ResumeTiming();
BigDecimal decimal(y);
benchmark::DoNotOptimize(decimal);
benchmark::ClobberMemory();
}
state.SetComplexityN(state.range(0));
}
BENCHMARK(BM_BigDecimalParsing)
->RangeMultiplier(10)->Range(10, 10000000)->Complexity(benchmark::oN);
static void BM_BigIntegerMultiply(benchmark::State &state) {
string x(state.range(0), '0');
for (auto _ : state) {
state.PauseTiming();
generate_random_digits(x);
BigInteger integer(x);
state.ResumeTiming();
BigInteger result = integer * integer;
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
state.SetComplexityN(state.range(0));
}
BENCHMARK(BM_BigIntegerMultiply)
->RangeMultiplier(10)->Range(10, 1000000)->Complexity(benchmark::oNLogN);
BENCHMARK_MAIN();