From 7b696b5f32f84fbfb185ef572ea7c646267dbaa7 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Tue, 13 Dec 2022 16:50:30 +1030 Subject: [PATCH] Add timers for no particular reason --- 2022/day13.py | 8 +++++++- 2022/helpers.py | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/2022/day13.py b/2022/day13.py index ae77436..d26bc60 100644 --- a/2022/day13.py +++ b/2022/day13.py @@ -1,5 +1,7 @@ from helpers import * +t0 = perf_counter_ns() list_pairs = [[eval(l) for l in pair.split('\n')] for pair in read_day(day).split('\n\n')] +t1 = perf_counter_ns() def compare_lists(l1, l2) -> int: for e1, e2 in zip(l1, l2): @@ -39,6 +41,10 @@ def sort_all_and_find_dividers(list_pairs): flat.sort(key=cmp_to_key(compare_lists)) return prod((flat.index(spacer) + 1 for spacer in spacers)) - print(f'Part 1: {count_ordered_pairs(list_pairs)}') print(f'Part 2: {sort_all_and_find_dividers(list_pairs)}') +t2 = perf_counter_ns() + +print_time('Parsing', t1-t0) +print_time('Computing', t2-t1) +print_time('Full', t2-t0) diff --git a/2022/helpers.py b/2022/helpers.py index a8d9301..353f3db 100644 --- a/2022/helpers.py +++ b/2022/helpers.py @@ -2,6 +2,7 @@ from numpy.typing import ArrayLike import numpy as np from functools import cmp_to_key from math import prod +from time import perf_counter_ns import re import requests import sys @@ -40,6 +41,9 @@ def read_day(day: int) -> str: with open(f'input/{day:02}', 'r') as file: return file.read().strip() +def print_time(name: str, ns: int): + print(f'{name} took {ns}ns = {ns/1000000:.2f}ms') + dtype = np.int32 directions_array = np.array([[1,0], [-1,0], [0,1], [0,-1]], dtype=dtype) directions_dict = {'R': directions_array[0], 'L': directions_array[1], 'D': directions_array[2], 'U': directions_array[3]} # Positive down for visualization