2016-06-02 05:08:01 +08:00
|
|
|
#undef NDEBUG
|
2016-05-19 01:59:34 +08:00
|
|
|
#include <algorithm>
|
2016-10-08 02:04:50 +08:00
|
|
|
#include <cassert>
|
2016-06-04 00:33:17 +08:00
|
|
|
#include <cmath>
|
2016-10-08 02:04:50 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <vector>
|
|
|
|
#include "benchmark/benchmark.h"
|
|
|
|
#include "output_test.h"
|
2016-05-19 01:59:34 +08:00
|
|
|
|
2016-06-02 05:08:01 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
#define ADD_COMPLEXITY_CASES(...) \
|
2016-10-08 02:04:50 +08:00
|
|
|
int CONCAT(dummy, __LINE__) = AddComplexityTest(__VA_ARGS__)
|
|
|
|
|
|
|
|
int AddComplexityTest(std::string big_o_test_name, std::string rms_test_name,
|
|
|
|
std::string big_o) {
|
|
|
|
SetSubstitutions({{"%bigo_name", big_o_test_name},
|
|
|
|
{"%rms_name", rms_test_name},
|
2016-10-08 11:56:22 +08:00
|
|
|
{"%bigo_str", "[ ]* %float " + big_o},
|
2016-10-08 02:04:50 +08:00
|
|
|
{"%bigo", big_o},
|
|
|
|
{"%rms", "[ ]*[0-9]+ %"}});
|
|
|
|
AddCases(
|
|
|
|
TC_ConsoleOut,
|
|
|
|
{{"^%bigo_name %bigo_str %bigo_str[ ]*$"},
|
|
|
|
{"^%bigo_name", MR_Not}, // Assert we we didn't only matched a name.
|
|
|
|
{"^%rms_name %rms %rms[ ]*$", MR_Next}});
|
|
|
|
AddCases(TC_JSONOut, {{"\"name\": \"%bigo_name\",$"},
|
Json reporter: don't cast floating-point to int; adjust tooling (#426)
* Json reporter: passthrough fp, don't cast it to int; adjust tooling
Json output format is generally meant for further processing
using some automated tools. Thus, it makes sense not to
intentionally limit the precision of the values contained
in the report.
As it can be seen, FormatKV() for doubles, used %.2f format,
which was meant to preserve at least some of the precision.
However, before that function is ever called, the doubles
were already cast to the integer via RoundDouble()...
This is also the case for console reporter, where it makes
sense because the screen space is limited, and this reporter,
however the CSV reporter does output some( decimal digits.
Thus i can only conclude that the loss of the precision
was not really considered, so i have decided to adjust the
code of the json reporter to output the full fp precision.
There can be several reasons why that is the right thing
to do, the bigger the time_unit used, the greater the
precision loss, so i'd say any sort of further processing
(like e.g. tools/compare_bench.py does) is best done
on the values with most precision.
Also, that cast skewed the data away from zero, which
i think may or may not result in false- positives/negatives
in the output of tools/compare_bench.py
* Json reporter: FormatKV(double): address review note
* tools/gbench/report.py: skip benchmarks with different time units
While it may be useful to teach it to operate on the
measurements with different time units, which is now
possible since floats are stored, and not the integers,
but for now at least doing such a sanity-checking
is better than providing misinformation.
2017-07-25 07:13:55 +08:00
|
|
|
{"\"cpu_coefficient\": %float,$", MR_Next},
|
|
|
|
{"\"real_coefficient\": %float,$", MR_Next},
|
2016-10-08 02:04:50 +08:00
|
|
|
{"\"big_o\": \"%bigo\",$", MR_Next},
|
|
|
|
{"\"time_unit\": \"ns\"$", MR_Next},
|
|
|
|
{"}", MR_Next},
|
|
|
|
{"\"name\": \"%rms_name\",$"},
|
2016-10-08 12:26:01 +08:00
|
|
|
{"\"rms\": %float$", MR_Next},
|
2016-10-08 02:04:50 +08:00
|
|
|
{"}", MR_Next}});
|
|
|
|
AddCases(TC_CSVOut, {{"^\"%bigo_name\",,%float,%float,%bigo,,,,,$"},
|
|
|
|
{"^\"%bigo_name\"", MR_Not},
|
|
|
|
{"^\"%rms_name\",,%float,%float,,,,,,$", MR_Next}});
|
2016-06-02 05:08:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
// ========================================================================= //
|
|
|
|
// --------------------------- Testing BigO O(1) --------------------------- //
|
|
|
|
// ========================================================================= //
|
|
|
|
|
2016-05-19 01:59:34 +08:00
|
|
|
void BM_Complexity_O1(benchmark::State& state) {
|
2017-10-18 02:17:02 +08:00
|
|
|
for (auto _ : state) {
|
2016-10-08 02:04:50 +08:00
|
|
|
for (int i = 0; i < 1024; ++i) {
|
|
|
|
benchmark::DoNotOptimize(&i);
|
|
|
|
}
|
2016-05-19 01:59:34 +08:00
|
|
|
}
|
2016-08-05 03:30:14 +08:00
|
|
|
state.SetComplexityN(state.range(0));
|
2016-05-19 01:59:34 +08:00
|
|
|
}
|
2016-10-08 02:04:50 +08:00
|
|
|
BENCHMARK(BM_Complexity_O1)->Range(1, 1 << 18)->Complexity(benchmark::o1);
|
|
|
|
BENCHMARK(BM_Complexity_O1)->Range(1, 1 << 18)->Complexity();
|
2018-04-12 22:40:24 +08:00
|
|
|
BENCHMARK(BM_Complexity_O1)->Range(1, 1 << 18)->Complexity([](int64_t) {
|
2016-10-08 02:04:50 +08:00
|
|
|
return 1.0;
|
|
|
|
});
|
|
|
|
|
|
|
|
const char *big_o_1_test_name = "BM_Complexity_O1_BigO";
|
|
|
|
const char *rms_o_1_test_name = "BM_Complexity_O1_RMS";
|
|
|
|
const char *enum_big_o_1 = "\\([0-9]+\\)";
|
|
|
|
// FIXME: Tolerate both '(1)' and 'lgN' as output when the complexity is auto
|
|
|
|
// deduced.
|
2016-08-10 04:14:15 +08:00
|
|
|
// See https://github.com/google/benchmark/issues/272
|
2016-10-08 02:04:50 +08:00
|
|
|
const char *auto_big_o_1 = "(\\([0-9]+\\))|(lgN)";
|
|
|
|
const char *lambda_big_o_1 = "f\\(N\\)";
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// Add enum tests
|
2016-08-29 03:24:16 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_1_test_name, rms_o_1_test_name, enum_big_o_1);
|
2016-06-02 05:08:01 +08:00
|
|
|
|
2016-07-23 07:31:05 +08:00
|
|
|
// Add auto enum tests
|
2016-08-29 03:24:16 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_1_test_name, rms_o_1_test_name, auto_big_o_1);
|
2016-07-23 07:31:05 +08:00
|
|
|
|
2016-06-02 05:08:01 +08:00
|
|
|
// Add lambda tests
|
2016-08-29 03:24:16 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_1_test_name, rms_o_1_test_name, lambda_big_o_1);
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// ========================================================================= //
|
|
|
|
// --------------------------- Testing BigO O(N) --------------------------- //
|
|
|
|
// ========================================================================= //
|
|
|
|
|
2018-04-04 06:12:47 +08:00
|
|
|
std::vector<int> ConstructRandomVector(int64_t size) {
|
2016-06-02 05:08:01 +08:00
|
|
|
std::vector<int> v;
|
2018-04-04 06:12:47 +08:00
|
|
|
v.reserve(static_cast<int>(size));
|
2016-06-02 05:08:01 +08:00
|
|
|
for (int i = 0; i < size; ++i) {
|
2016-08-29 03:24:16 +08:00
|
|
|
v.push_back(std::rand() % size);
|
2016-06-02 05:08:01 +08:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
2016-05-19 01:59:34 +08:00
|
|
|
|
2016-06-02 05:08:01 +08:00
|
|
|
void BM_Complexity_O_N(benchmark::State& state) {
|
2016-08-05 03:30:14 +08:00
|
|
|
auto v = ConstructRandomVector(state.range(0));
|
2018-04-04 06:12:47 +08:00
|
|
|
// Test worst case scenario (item not in vector)
|
|
|
|
const int64_t item_not_in_vector = state.range(0) * 2;
|
2017-10-18 02:17:02 +08:00
|
|
|
for (auto _ : state) {
|
2016-10-08 02:04:50 +08:00
|
|
|
benchmark::DoNotOptimize(std::find(v.begin(), v.end(), item_not_in_vector));
|
2016-05-19 01:59:34 +08:00
|
|
|
}
|
2016-08-05 03:30:14 +08:00
|
|
|
state.SetComplexityN(state.range(0));
|
2016-05-19 01:59:34 +08:00
|
|
|
}
|
2016-10-08 02:04:50 +08:00
|
|
|
BENCHMARK(BM_Complexity_O_N)
|
|
|
|
->RangeMultiplier(2)
|
|
|
|
->Range(1 << 10, 1 << 16)
|
|
|
|
->Complexity(benchmark::oN);
|
|
|
|
BENCHMARK(BM_Complexity_O_N)
|
|
|
|
->RangeMultiplier(2)
|
|
|
|
->Range(1 << 10, 1 << 16)
|
2018-04-12 22:40:24 +08:00
|
|
|
->Complexity([](int64_t n) -> double { return n; });
|
2016-10-08 02:04:50 +08:00
|
|
|
BENCHMARK(BM_Complexity_O_N)
|
|
|
|
->RangeMultiplier(2)
|
|
|
|
->Range(1 << 10, 1 << 16)
|
|
|
|
->Complexity();
|
|
|
|
|
|
|
|
const char *big_o_n_test_name = "BM_Complexity_O_N_BigO";
|
|
|
|
const char *rms_o_n_test_name = "BM_Complexity_O_N_RMS";
|
|
|
|
const char *enum_auto_big_o_n = "N";
|
|
|
|
const char *lambda_big_o_n = "f\\(N\\)";
|
2016-05-25 04:25:59 +08:00
|
|
|
|
2016-06-02 05:08:01 +08:00
|
|
|
// Add enum tests
|
2016-08-29 03:24:16 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_n_test_name, rms_o_n_test_name, enum_auto_big_o_n);
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// Add lambda tests
|
2016-08-29 03:24:16 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_n_test_name, rms_o_n_test_name, lambda_big_o_n);
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// ========================================================================= //
|
|
|
|
// ------------------------- Testing BigO O(N*lgN) ------------------------- //
|
|
|
|
// ========================================================================= //
|
2016-05-19 01:59:34 +08:00
|
|
|
|
|
|
|
static void BM_Complexity_O_N_log_N(benchmark::State& state) {
|
2016-08-05 03:30:14 +08:00
|
|
|
auto v = ConstructRandomVector(state.range(0));
|
2017-10-18 02:17:02 +08:00
|
|
|
for (auto _ : state) {
|
2016-10-08 02:04:50 +08:00
|
|
|
std::sort(v.begin(), v.end());
|
2016-05-19 01:59:34 +08:00
|
|
|
}
|
2016-08-05 03:30:14 +08:00
|
|
|
state.SetComplexityN(state.range(0));
|
2016-05-19 01:59:34 +08:00
|
|
|
}
|
2016-10-08 02:04:50 +08:00
|
|
|
BENCHMARK(BM_Complexity_O_N_log_N)
|
|
|
|
->RangeMultiplier(2)
|
|
|
|
->Range(1 << 10, 1 << 16)
|
|
|
|
->Complexity(benchmark::oNLogN);
|
|
|
|
BENCHMARK(BM_Complexity_O_N_log_N)
|
|
|
|
->RangeMultiplier(2)
|
|
|
|
->Range(1 << 10, 1 << 16)
|
2018-04-12 22:40:24 +08:00
|
|
|
->Complexity([](int64_t n) { return n * log2(n); });
|
2016-10-08 02:04:50 +08:00
|
|
|
BENCHMARK(BM_Complexity_O_N_log_N)
|
|
|
|
->RangeMultiplier(2)
|
|
|
|
->Range(1 << 10, 1 << 16)
|
|
|
|
->Complexity();
|
|
|
|
|
|
|
|
const char *big_o_n_lg_n_test_name = "BM_Complexity_O_N_log_N_BigO";
|
|
|
|
const char *rms_o_n_lg_n_test_name = "BM_Complexity_O_N_log_N_RMS";
|
|
|
|
const char *enum_auto_big_o_n_lg_n = "NlgN";
|
|
|
|
const char *lambda_big_o_n_lg_n = "f\\(N\\)";
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// Add enum tests
|
2016-10-08 02:04:50 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_n_lg_n_test_name, rms_o_n_lg_n_test_name,
|
|
|
|
enum_auto_big_o_n_lg_n);
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// Add lambda tests
|
2016-10-08 02:04:50 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_n_lg_n_test_name, rms_o_n_lg_n_test_name,
|
|
|
|
lambda_big_o_n_lg_n);
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// ========================================================================= //
|
|
|
|
// --------------------------- TEST CASES END ------------------------------ //
|
|
|
|
// ========================================================================= //
|
|
|
|
|
2016-10-08 02:04:50 +08:00
|
|
|
int main(int argc, char *argv[]) { RunOutputTests(argc, argv); }
|