multiprocessing&threading心得
环境:python3.7.0
系统信息:Code:
import time
from multiprocessing import Process
import threading
def Solve(n:int)->int:
def Factorial(n:int)->int:
factorial=1
for i in range(1,n+1):
factorial*=I
return factorial
def Sum(n:int)->int:
sum=0
for i in str(n):
sum+=int(i)
return sum
sum=0
for i in range(1,n+1):
sum+=Sum(Factorial(i))
return sum
def useProcess():
start=time.time()
Process(target=Solve,args=(10000,)).start()
end=time.time()
return end-start
def useNone():
start=time.time()
Solve(10000)
end=time.time()
return end-start
def useThread():
start=time.time()
threading.Thread(target=Solve,args=(10000,)).start()
end=time.time()
return end-start
def main():
print("useProcess:", useProcess(), 's')
threading.Event().wait(10)
print("useThread:",useThread(),'s')
threading.Event().wait(10)
print("useNone:",useNone(),'s')
if __name__ == '__main__':
main()
运行结果:
Thread:
threading库
Process:
multiprocessing库
不开线程与进程与开了线程或进程的差了至少35k倍