diff --git a/src/common/timer.h b/src/common/timer.h index cc183a1..85eac3c 100644 --- a/src/common/timer.h +++ b/src/common/timer.h @@ -3,10 +3,13 @@ #include #include +#include +#include +#include class Timer { public: - Timer() { + explicit Timer(const int iter = 1) : iterations(iter) { running = true; start = std::chrono::high_resolution_clock::now(); } @@ -16,18 +19,26 @@ public: running = false; }; + void add_point(const std::string &name) { + points[name] = std::chrono::high_resolution_clock::now(); + } + void print () { if (running) { stop(); }; - const auto seconds = std::chrono::duration_cast(end - start); - const auto ms = std::chrono::duration_cast(end - start); - const auto us = std::chrono::duration_cast(end - start); - std::cout << "\n# Execution time # \n\t" << seconds.count() << " s\n\t" << ms.count() << " ms\n\t" << us.count() << " us" << std::endl; + + std::cout << "\n# Execution time # \n"; + for (const auto &[name, time] : points) { + std::cout << "\t" << name << ": " << std::chrono::duration_cast(time - start).count() << " ms | " << std::chrono::duration_cast(time - start).count() << " us" << std::endl; + } + std::cout << "Global: " << std::chrono::duration_cast(end - start).count() / iterations << " ms | " << std::chrono::duration_cast(end-start).count() / iterations << " us" << std::endl; } private: bool running = false; std::chrono::time_point start; std::chrono::time_point end; + std::unordered_map> points; + int iterations; }; #endif //ADVENTOFCODE2025_TIMER_H \ No newline at end of file diff --git a/src/day1/p1/faf.cpp b/src/day1/p1/faf.cpp index c3d5f35..43a47f5 100644 --- a/src/day1/p1/faf.cpp +++ b/src/day1/p1/faf.cpp @@ -66,43 +66,48 @@ #include "timer.h" int main() { - Timer timer; - - std::ifstream stream("input/day1/p1.txt", std::ios::binary | std::ios::ate); - const auto size = stream.tellg(); - stream.seekg(0); - std::vector buffer(size); - stream.read(buffer.data(), size); - stream.close(); - - int state = 50; + constexpr int iterations = 1000; + Timer timer(iterations); int password = 0; - const char* ptr = buffer.data(); - const char* end = ptr + size; + for (int i = 0; i < iterations; ++i) { + std::ifstream stream("input/day1/p1.txt", std::ios::binary | std::ios::ate); + const auto size = stream.tellg(); + stream.seekg(0); + std::vector buffer(size); + stream.read(buffer.data(), size); - while (ptr < end) { - const bool is_right = (*ptr == 'R'); - ++ptr; + int state = 50; - int number; - auto [next, ec] = std::from_chars(ptr, end, number); - ptr = next; - if (ptr < end && *ptr == '\r') ++ptr; - if (ptr < end && *ptr == '\n') ++ptr; + const char* ptr = buffer.data(); + const char* end = ptr + size; - if (is_right) { - state = (state + number) % 100; - } else { - state = (state + 100 - number % 100) % 100; + while (ptr < end) { + const bool is_right = (*ptr == 'R'); + ++ptr; + + int number; + auto [next, ec] = std::from_chars(ptr, end, number); + ptr = next; + + if (ptr < end && *ptr == '\r') ++ptr; + if (ptr < end && *ptr == '\n') ++ptr; + + if (is_right) { + state = (state + number) % 100; + } else { + state = (state + 100 - number % 100) % 100; + } + + password += (state == 0); } - password += (state == 0); + stream.close(); } timer.stop(); - std::cout << "Password: " << password << std::endl; + std::cout << "Password: " << password / iterations << std::endl; timer.print();