-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove_zeroes_benchmark.cpp
89 lines (70 loc) · 3.38 KB
/
move_zeroes_benchmark.cpp
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
82
83
84
85
86
87
88
89
// Copyright (c) Omar Boukli-Hacene. All rights reserved.
// Distributed under an MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT
#include <array>
#include <tuple>
#include <catch2/catch_test_macros.hpp>
#include <nanobench.h>
#include <nameof.hpp>
#include "forfun/move_zeroes.hpp"
TEST_CASE("Move zeroes benchmarking", "[benchmark][move_zeroes]")
{
using namespace forfun::move_zeroes;
using ContainerType = std::array<int, 128U>;
using Itr = ContainerType::iterator;
ankerl::nanobench::Bench()
.title("Move zeroes")
.relative(true)
.run(
NAMEOF_RAW(sol1::move_zeroes<Itr, Itr>).c_str(),
[]() noexcept {
std::array nums{
0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0,
1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 1, 0,
0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0,
1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 1, 0,
0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0,
1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 1, 0,
0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0,
1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 1, 0,
};
static_assert(nums.size() == std::tuple_size_v<ContainerType>);
sol1::move_zeroes(nums.begin(), nums.end());
ankerl::nanobench::doNotOptimizeAway(nums);
}
)
.run(
NAMEOF_RAW(sol2::move_zeroes<Itr, Itr>).c_str(),
[]() noexcept {
std::array nums{
0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0,
1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 1, 0,
0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0,
1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 1, 0,
0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0,
1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 1, 0,
0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0,
1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 1, 0,
};
static_assert(nums.size() == std::tuple_size_v<ContainerType>);
sol2::move_zeroes(nums.begin(), nums.end());
ankerl::nanobench::doNotOptimizeAway(nums);
}
)
.run(NAMEOF_RAW(stl::move_zeroes<Itr, Itr>).c_str(), []() noexcept {
std::array nums{
0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0,
1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 1, 0,
0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0,
1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 1, 0,
0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0,
1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 1, 0,
0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 0,
1, 0, 3, 12, 0, 1, 0, 3, 12, 0, 1, 0, 3, 12, 1, 0,
};
static_assert(nums.size() == std::tuple_size_v<ContainerType>);
stl::move_zeroes(nums.begin(), nums.end());
ankerl::nanobench::doNotOptimizeAway(nums);
});
}