From bf896841d207107fd3ee0e52ff0c840ab2531219 Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 5 Dec 2023 10:49:36 +0100 Subject: [PATCH] add: multhread --- 2023/day05/src/part2.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/2023/day05/src/part2.py b/2023/day05/src/part2.py index 6908dd4..1ef3549 100644 --- a/2023/day05/src/part2.py +++ b/2023/day05/src/part2.py @@ -1,7 +1,7 @@ import threading import time -text: str = open("example.txt", "r").read() +text: str = open("input.txt", "r").read() _value: int = 0 @@ -57,19 +57,19 @@ def get_min_in_range(lst_conversions, start, stop): min_value = do_all_convertions(lst_conversions, start) i = start + 1; while (i != stop): - - if ((i - start) % 10000 == 0): - display_duration(10000, stop - start) - print(i - start, '/', stop - start) - value = do_all_convertions(lst_conversions, i) #print(min_value, value, min(value, min_value)) min_value = min(value, min_value) i += 1 return (min_value) -CHUNK_SIZE = 10 values = [] + +def thread_process(convertions, start_value, chunk): + global values + values.append(get_min_in_range(convertions, start_value, chunk)) + +CHUNK_SIZE = 10 for current_range in current_ranges: start, stop = current_range i = start @@ -83,7 +83,10 @@ for current_range in current_ranges: start_value = start i = 0 while (i < len(chunks)): - values.append(get_min_in_range(convertions, start_value, chunks[i])) + t = threading.Thread(target=thread_process, args=(convertions, start_value, chunks[i])) + t.start() + display_duration(1, len(chunks)) + print(i, '/', len(chunks)) i += 1 start_value = chunks[i - 1]