diff --git a/CMakeLists.txt b/CMakeLists.txt index bf8aabf..20213e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,14 +74,17 @@ add_executable( ) target_link_libraries(tests PRIVATE Catch2::Catch2WithMain) +find_package(fmt CONFIG REQUIRED) find_package(nameof CONFIG REQUIRED) find_package(nanobench CONFIG REQUIRED) add_executable( benchmark "benchmark/benchmark.cpp" + "benchmark/lc_lru_cache_benchmark.cpp" "benchmark/palindrome_benchmark.cpp" ) target_link_libraries( benchmark PRIVATE Catch2::Catch2WithMain + PRIVATE fmt::fmt ) diff --git a/benchmark/lc_lru_cache_benchmark.cpp b/benchmark/lc_lru_cache_benchmark.cpp new file mode 100644 index 0000000..7177e0b --- /dev/null +++ b/benchmark/lc_lru_cache_benchmark.cpp @@ -0,0 +1,96 @@ +// 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 + +#include + +#include + +#include + +#include "forfun/lc_lru_cache.hpp" + +template +std::enable_if_t, void> +test(int const capacity) { + int volatile val{}; + T cache(capacity); + + for (int i{0}; i < capacity; ++i) { + cache.put(i, i); + } + + val = cache.get(1); + + val = cache.get(2); + + val = cache.get(3); + + cache.put(capacity + 1, capacity + 1); + + val = cache.get(1); + + val = cache.get(4); + + val = cache.get(2); + + cache.put(5, 5); + + val = cache.get(3); + + for (int i{0}; i < capacity; ++i) { + val = cache.get(i); + } +} + +TEST_CASE("forfun::lrucache benchmarking") { + SECTION("small") { + constexpr int const lrucache_capacity{32}; + + ankerl::nanobench::Bench() + + .title( + fmt::format("LRU Cache with {} cache items", lrucache_capacity)) + + .run( + NAMEOF_FULL_TYPE(forfun::lrucache::stl::LRUCache).data(), + []() { + test(lrucache_capacity); + }) + + .run( + NAMEOF_FULL_TYPE(forfun::lrucache::naive::LRUCache).data(), + []() { + test(lrucache_capacity); + }) + + ; + } + + SECTION("large") { + constexpr int const lrucache_capacity{128}; + + ankerl::nanobench::Bench() + + .title( + fmt::format("LRU Cache with {} cache items", lrucache_capacity)) + + .run( + NAMEOF_FULL_TYPE(forfun::lrucache::stl::LRUCache).data(), + []() { + test(lrucache_capacity); + }) + + .run( + NAMEOF_FULL_TYPE(forfun::lrucache::naive::LRUCache).data(), + []() { + test(lrucache_capacity); + }) + + ; + } +} diff --git a/vcpkg.json b/vcpkg.json index afa2d39..6ec40b4 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,6 +1,7 @@ { "dependencies": [ "catch2", + "fmt", "nameof", "nanobench" ]