我有一个任务可以加快此脚本的执行时间。目标低于12秒。 该代码可以完成工作,但是效率并不高。我该如何改善呢? 我怀疑我不是在这里最好的方式使用多处理模块:
import multiprocessing, datetime
from time import sleep
def queue_time(customers, num_of_tills):
if len(customers) == 0:
return 0
start = datetime.datetime.now()
for x in range(num_of_tills):
for y in customers:
process = multiprocessing.Process(target=sleep, args=(y,))
process.start()
process.join()
result = datetime.datetime.now() - start
return result.seconds
建议的编辑:
import multiprocessing, datetime
from time import sleep
def queue_time(customers, num_of_tills):
if len(customers) == 0:
return 0
start = datetime.datetime.now()
for x in range(num_of_tills):
for y in customers:
process = multiprocessing.Process(target=sleep, args=(y,))
process.start()
process.join()
result = datetime.datetime.now() - start
return result.seconds
请假定customers
参数是整数列表。