from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import time
defcalctime(x):
start = time.perf_counter()
x()
end = time.perf_counter()return("time cost is", end-start)deffun(x):
a, b = x
time.delay(2)return('a is ', a,'b is ', b)defmuti_thread1():with ThreadPoolExecutor()as pool:
data =[(x, x)for x inrange(50)]
results = pool.map(fun, data)defthread():
data =[(x, x)for x inrange(50)]for i in data:
fun(i)defmuti_process():with ProcessPoolExecutor()as pool:
data =[(x, x)for x inrange(50)]
results = pool.map(fun, data)if __name__ =='__main__':print(calctime(muti_thread1))print(calctime(thread))print(calctime(muti_process))