Add timers for no particular reason

This commit is contained in:
Luke Hubmayer-Werner 2022-12-13 16:50:30 +10:30
parent c1cf34491a
commit 7b696b5f32
2 changed files with 11 additions and 1 deletions

View File

@ -1,5 +1,7 @@
from helpers import * 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')] 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: def compare_lists(l1, l2) -> int:
for e1, e2 in zip(l1, l2): 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)) flat.sort(key=cmp_to_key(compare_lists))
return prod((flat.index(spacer) + 1 for spacer in spacers)) return prod((flat.index(spacer) + 1 for spacer in spacers))
print(f'Part 1: {count_ordered_pairs(list_pairs)}') print(f'Part 1: {count_ordered_pairs(list_pairs)}')
print(f'Part 2: {sort_all_and_find_dividers(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)

View File

@ -2,6 +2,7 @@ from numpy.typing import ArrayLike
import numpy as np import numpy as np
from functools import cmp_to_key from functools import cmp_to_key
from math import prod from math import prod
from time import perf_counter_ns
import re import re
import requests import requests
import sys import sys
@ -40,6 +41,9 @@ def read_day(day: int) -> str:
with open(f'input/{day:02}', 'r') as file: with open(f'input/{day:02}', 'r') as file:
return file.read().strip() return file.read().strip()
def print_time(name: str, ns: int):
print(f'{name} took {ns}ns = {ns/1000000:.2f}ms')
dtype = np.int32 dtype = np.int32
directions_array = np.array([[1,0], [-1,0], [0,1], [0,-1]], dtype=dtype) 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 directions_dict = {'R': directions_array[0], 'L': directions_array[1], 'D': directions_array[2], 'U': directions_array[3]} # Positive down for visualization