Add timer.h for benchmarking

This commit is contained in:
2025-12-01 03:28:00 -05:00
parent 5d5d24168c
commit 66c39108ce
4 changed files with 49 additions and 2 deletions

View File

@@ -22,6 +22,7 @@ ENDIF()
### Copy the input files dir to build
file(COPY ${INPUT_DIR} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
include_directories(src/common)
### EXECUTABLES

33
src/common/timer.h Normal file
View File

@@ -0,0 +1,33 @@
#ifndef ADVENTOFCODE2025_TIMER_H
#define ADVENTOFCODE2025_TIMER_H
#include <chrono>
#include <iostream>
class Timer {
public:
Timer() {
running = true;
start = std::chrono::high_resolution_clock::now();
}
void stop() {
end = std::chrono::high_resolution_clock::now();
running = false;
};
void print () {
if (running) { stop(); };
const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(end - start);
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
const auto us = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "Execution time: \n\t" << seconds.count() << " s\n\t" << ms.count() << " ms\n\t" << us.count() << " us" << std::endl;
}
private:
bool running = false;
std::chrono::time_point<std::chrono::system_clock> start;
std::chrono::time_point<std::chrono::system_clock> end;
};
#endif //ADVENTOFCODE2025_TIMER_H

View File

@@ -63,6 +63,7 @@
#include <string>
#include <fstream>
#include <memory>
#include "timer.h"
bool get_dir(const std::string& line) {
if (line.find('R') != std::string::npos) {
@@ -96,6 +97,8 @@ int rotate (const int state, const bool direction, const int number) {
}
int main() {
Timer timer;
std::ifstream stream("input/day1/p1.txt");
std::string line;
int state = 50;
@@ -108,7 +111,11 @@ int main() {
}
}
timer.stop();
std::cout << "Password: " << password << std::endl;
timer.print();
return 0;
}

View File

@@ -95,8 +95,8 @@
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <chrono>
#include "timer.h"
bool get_dir(const std::string& line) {
if (line.find('R') != std::string::npos) {
@@ -140,6 +140,8 @@ int get_number(const std::string& line) {
}
int main() {
Timer timer;
std::ifstream stream("input/day1/p1.txt");
std::string line;
int state = 50;
@@ -151,7 +153,11 @@ int main() {
password = std::get<1>(temp);
}
timer.stop();
std::cout << "Password: " << password << std::endl;
timer.print();
return 0;
}