Skip to content

Commit

Permalink
Merge pull request #66 from EduardGomezEscandell/timeit
Browse files Browse the repository at this point in the history
Time solutions
  • Loading branch information
EduardGomezEscandell authored Dec 1, 2023
2 parents f2571c0 + 271bba4 commit afbd3d6
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions 2023/xmaslib/solution/solution.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
#include "solution.hpp"

#include <chrono>
#include <format>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>

#include <unistd.h>

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<std::chrono::microseconds>(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<std::chrono::microseconds>(elapsed).count());
} catch (std::runtime_error &err) {
std::cerr << "Part 2 failed with message: " << err.what() << std::endl;
}
Expand Down

0 comments on commit afbd3d6

Please sign in to comment.