新手求教:threading不节省时间,意义何在?
为了学习threading,我制作了两组代码来对比效果(一组是四线程并发,另一组是按顺序一个一个执行):代码1:
import threading
import time
def 玩(t):
for j in range(100000):
s=1.0;x=1.1
for i in range(170):
s=s*x
x=x+1
print(time.time()-t,'\t')
t1=time.time()
th1=threading.Thread(target=玩,args=(t1,))
th2=threading.Thread(target=玩,args=(t1,))
th3=threading.Thread(target=玩,args=(t1,))
th4=threading.Thread(target=玩,args=(t1,))
th1.start()
th2.start()
th3.start()
th4.start()
代码2:
import threading
import time
def 玩(t):
for j in range(100000):
s=1.0;x=1.1
for i in range(170):
s=s*x
x=x+1
print(time.time()-t,'\t')
t1=time.time()
玩(t1)
玩(t1)
玩(t1)
玩(t1)
结果1:
4.004648208618164
4.4277307987213135
5.130835771560669
5.193334579467773
结果2:
1.330603837966919
2.654576301574707
3.99839186668396
5.322086572647095
四线程并发与单线程执行的总用时差不多,这样的多线程有什么用?(我的cpu是四核心四线程)