Here an example using multi-threading. Inside your run
function you can actually do whatever you want in a multi-threading fashion.
import threading
class my_thread(threading.Thread):
def __init__(self, strings, indexes, thread_id=0):
super().__init__()
self.strings = strings
self.indexes = indexes
self.avg_score = []
self.id = thread_id
def run(self):
print(f'Thread {self.id} working: from {self.indexes[0]} to {self.indexes[-1]}')
for i in self.indexes:
if i > len(self.strings):
break
count = 0
score = 0
for j in range(len(self.strings)):
if i == j:
continue
score += compare(self.strings[i], self.strings[j])
count += 1
self.avg_score.append(score/count)
# do other stuff
print(f'Thread {self.id} done')
avg_score = []
strings = get_strings()
n = len(strings)
# Set the number of threads
num_threads = 10
threads = []
for ii in range(num_threads):
threads.append(my_thread(strings, range(n//num_threads*ii, n//num_threads*(ii+1)), thread_id=ii))
threads[-1].start()
for t in threads:
t.join()
avg_score.extend(t.avg_score)
Here a reproducible example:
import threading
import time
import random
import string
class my_thread(threading.Thread):
def __init__(self, strings, indexes, thread_id=0):
super().__init__()
self.strings = strings
self.indexes = indexes
self.avg_score = []
self.id = thread_id
def run(self):
print(f'Thread {self.id} working: from {self.indexes[0]} to {self.indexes[-1]}')
for i in self.indexes:
if i > len(self.strings):
break
count = 0
score = 0
for j in range(len(self.strings)):
if i == j:
continue
score += compare(self.strings[i], self.strings[j])
count += 1
self.avg_score.append(score/count)
# do other stuff
print(f'Thread {self.id} done')
def compare(str1, str2):
time.sleep(0.01)
return random.random()
strings = [''.join(random.choices(string.ascii_uppercase + string.digits, k=10)) for _ in range(100)]
n = len(strings)
# --- MULTI-THREADING ---
avg_score = []
# Set the number of threads
num_threads = 10
start_time = time.time()
threads = []
for ii in range(num_threads):
threads.append(my_thread(strings, range(n//num_threads*ii, n//num_threads*(ii+1)), thread_id=ii))
threads[-1].start()
for t in threads:
t.join()
avg_score.extend(t.avg_score)
print(f'Elapsed time with multi-threading: {time.time() - start_time} s')
# --- SINGLE THREAD ---
avg_score = []
start_time = time.time()
for i in range(n):
count = 0
score = 0
for j in range(n):
if i == j:
continue
score += compare(strings[i], strings[j])
count += 1
avg_score.append(score / count)
# do other stuff
print(f'Elapsed time with no threads: {time.time() - start_time} s')