Add timer.h for benchmarking
This commit is contained in:
33
src/common/timer.h
Normal file
33
src/common/timer.h
Normal 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
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user