So, ich dachte mir, dass ist doch mal etwas, bei dem man mit Multiprocessing noch mehr raus holen kann. Rausgekommen ist das hier.
Bei den von dir angegebenen 10000 Bereichen, verkürzt sich die 1 CPU Laufzeit von 16,5 sek bei 100 Prozessen auf unter 0,3 sek.
Bei 60000 Bereichen von solo 600 sek auf unter 2 sek.
Ich denke damit kannst du arbeiten, oder?
Code: Alles auswählen
import multiprocessing
import time
def check(sub_ranges):
keep = set(list(range(len(sub_ranges))))
lengths = [ende - start + 1 for start, ende in sub_ranges]
for i in range(len(sub_ranges) - 1):
a_start, a_ende = sub_ranges[i]
for j in range(i + 1, len(sub_ranges)):
b_start, b_ende = sub_ranges[j]
if not (a_ende < b_start or b_ende < a_start):
if lengths[i] >= lengths[j]:
keep.discard(j)
else:
keep.discard(i)
return [sub_ranges[index] for index in keep]
def check_all(ranges):
with multiprocessing.Pool() as pool:
return [result
for results in pool.map(check, ranges)
for result in results]
if __name__ == "__main__":
tasks = 100
ranges = [[0,2], [2,3], [6,8], [8, 15], [9, 13], [9,11], [7, 10], [9, 14], [7, 9], [-1, 0]] * (1000 // tasks)
sp_ranges = ranges * tasks
mp_ranges = [ranges] * tasks
start_time = time.time()
print('Final result', check(sp_ranges))
duration = time.time() - start_time
print(f"Solo Duration {duration} seconds")
start_time = time.time()
print('Final result', check(check_all(mp_ranges)))
duration = time.time() - start_time
print(f"MP Duration {duration} seconds")
10000 Bereiche, 10 Prozesse
Solo Duration 16.32785940170288 seconds
MP Duration 0.7143480777740479 seconds
10000 Bereiche, 20 Prozesse
Solo Duration 16.62518858909607 seconds
MP Duration 0.4435901641845703 seconds
10000 Bereiche, 40 Prozesse
Solo Duration 16.318793058395386 seconds
MP Duration 0.340346097946167 seconds
10000 Bereiche, 100 Prozesse
Solo Duration 16.29350256919861 seconds
MP Duration 0.28729987144470215 seconds
60000 Bereiche, 30 Prozesse
Solo Duration 601.739506483078 seconds
MP Duration 4.643344402313232 seconds
60000 Bereiche, 60 Prozesse
Solo Duration 601.739506483078 seconds
MP Duration 2.509606122970581 seconds
60000 Bereiche, 100 Prozesse
Solo Duration 601.739506483078 seconds
MP Duration 1.6860344409942627 seconds