From 271bba4632b63069b07eb0a791b0edd6c03badd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20G=C3=B3mez=20Escandell?= Date: Fri, 1 Dec 2023 11:58:13 +0100 Subject: [PATCH] Time solutions --- 2023/xmaslib/solution/solution.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/2023/xmaslib/solution/solution.cpp b/2023/xmaslib/solution/solution.cpp index 45f085a..410b183 100644 --- a/2023/xmaslib/solution/solution.cpp +++ b/2023/xmaslib/solution/solution.cpp @@ -1,25 +1,35 @@ #include "solution.hpp" +#include #include #include #include #include #include +#include + namespace xmas { void solution::run() { std::cout << "Day 1" << std::endl; + try { - const auto p1 = this->part1(); - std::cout << " Result 1: " << p1 << '\n'; + const auto start = std::chrono::high_resolution_clock::now(); + const auto result = this->part1(); + const auto elapsed = std::chrono::high_resolution_clock::now() - start; + + std::cout << std::format(" Result 1: {} ({} μs)\n", result, std::chrono::duration_cast(elapsed).count()); } catch (std::runtime_error &err) { std::cerr << "Part 1 failed with message: " << err.what() << std::endl; } try { - const auto p2 = this->part2(); - std::cout << " Result 2: " << p2 << '\n'; + const auto start = std::chrono::high_resolution_clock::now(); + const auto result = this->part2(); + const auto elapsed = std::chrono::high_resolution_clock::now() - start; + + std::cout << std::format(" Result 2: {} ({} μs)\n", result, std::chrono::duration_cast(elapsed).count()); } catch (std::runtime_error &err) { std::cerr << "Part 2 failed with message: " << err.what() << std::endl; }