-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// 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 <catch2/catch_test_macros.hpp> | ||
|
||
#include <nanobench.h> | ||
|
||
inline constexpr int const f{514229}; | ||
inline constexpr unsigned long const ful{f}; | ||
|
||
static_assert(ful == f); | ||
|
||
TEST_CASE( | ||
"fibonacci sequence loops benchmarking", "[benchmark][fibonacci_sequence]") | ||
{ | ||
using result_t = unsigned long; | ||
static_assert(sizeof(result_t) >= 8); | ||
|
||
ankerl::nanobench::Bench() | ||
.title("Fibonacci sequence loops") | ||
|
||
.run( | ||
"Three variables", | ||
[]() { | ||
result_t r{}; | ||
// clang-format off | ||
for (auto i{0}, j{1}, tmp{0}; | ||
i <= f; | ||
tmp = j + i, i = j, j = tmp) | ||
{ | ||
r += i; | ||
} | ||
// clang-format on | ||
ankerl::nanobench::doNotOptimizeAway(r); | ||
}) | ||
|
||
.run( | ||
"Two variables", | ||
[]() { | ||
result_t r{}; | ||
for (auto i{0}, j{1}; i <= f; j += i, i = j - i) | ||
{ | ||
r += i; | ||
} | ||
ankerl::nanobench::doNotOptimizeAway(r); | ||
}) | ||
|
||
.run( | ||
"Two unsigned long variables", | ||
[]() { | ||
result_t r{}; | ||
for (auto i{0ul}, j{1ul}; i <= ful; j += i, i = j - i) | ||
{ | ||
r += i; | ||
} | ||
ankerl::nanobench::doNotOptimizeAway(r); | ||
}) | ||
|
||
.run( | ||
"Two variables by Creel", | ||
[]() { | ||
result_t r{}; | ||
// Source: https://youtu.be/IZc4Odd3K2Q?t=949 | ||
for (auto i{0}, j{1}; i <= f; j = (i += j) - j) | ||
{ | ||
r += i; | ||
} | ||
ankerl::nanobench::doNotOptimizeAway(r); | ||
}) | ||
|
||
; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 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 | ||
|
||
/// Problem source: | ||
/// https://en.wikipedia.org/wiki/Fibonacci_sequence | ||
|
||
#ifndef FORFUN_FIBONACCI_SEQUENCE_HPP_ | ||
#define FORFUN_FIBONACCI_SEQUENCE_HPP_ | ||
|
||
namespace forfun::fibonacci::sequence { | ||
|
||
// See benchmark/fibonacci_benchmark.cpp | ||
|
||
} // namespace forfun::fibonacci::sequence | ||
|
||
#endif // FORFUN_FIBONACCI_SEQUENCE_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// 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 "forfun/fibonacci_sequence.hpp" | ||
|
||
// See benchmark/fibonacci_sequence_benchmark.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// 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 <catch2/catch_test_macros.hpp> | ||
|
||
#include "forfun/fibonacci_sequence.hpp" | ||
|
||
TEST_CASE("fibonacci_sequence") | ||
{ | ||
SECTION("Loops") | ||
{ | ||
SKIP("See benchmark/fibonacci_sequence_benchmark.cpp"); | ||
} | ||
} |