-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest_time_to_buy_and_sell_stock_benchmark.cpp
82 lines (68 loc) · 2.31 KB
/
best_time_to_buy_and_sell_stock_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
// 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 <vector>
#include <catch2/catch_test_macros.hpp>
#include <nanobench.h>
#include <nameof.hpp>
#include "forfun/best_time_to_buy_and_sell_stock.hpp"
TEST_CASE(
"Best time to buy and sell stock benchmarking",
"[benchmark][best_time_to_buy_and_sell_stock]"
)
{
using namespace forfun;
using Iter = std::vector<int>::const_iterator;
std::vector const prices{
2, 5, 11, 17, 23, 29, 41, 47, 53, 59, 71,
83, 89, 101, 107, 113, 131, 137, 149, 167, 173, 179,
191, 197, 227, 233, 239, 251, 257, 263, 269, 271,
};
ankerl::nanobench::Bench()
.title("Best time to buy and sell stock")
.relative(true)
.run(
NAMEOF_RAW(
best_time_to_buy_and_sell_stock::calc_max_profit<Iter, Iter>
)
.c_str(),
[&prices]() noexcept {
auto const volatile r{
best_time_to_buy_and_sell_stock::calc_max_profit(
prices.cbegin(), prices.cend()
)
};
ankerl::nanobench::doNotOptimizeAway(&r);
}
)
.run(
NAMEOF_RAW(
best_time_to_buy_and_sell_stock::optimized_l1::
calc_max_profit<Iter, Iter>
)
.c_str(),
[&prices]() noexcept {
auto const volatile r{
best_time_to_buy_and_sell_stock::optimized_l1::
calc_max_profit(prices.cbegin(), prices.cend())
};
ankerl::nanobench::doNotOptimizeAway(&r);
}
)
.run(
NAMEOF_RAW(
best_time_to_buy_and_sell_stock::optimized_l2::
calc_max_profit<Iter, Iter>
)
.c_str(),
[&prices]() noexcept {
auto const volatile r{
best_time_to_buy_and_sell_stock::optimized_l2::
calc_max_profit(prices.cbegin(), prices.cend())
};
ankerl::nanobench::doNotOptimizeAway(&r);
}
)
;
}