【发布时间】:2021-06-20 16:55:31
【问题描述】:
我有点玩弄多处理和不同的数学库来计算 pi,并想知道通过实现 time.perf_counter() 使用或不使用多处理它的速度有多快。 mp.Pool 映射 20 个线程与我的 CPU 线程数相同,在处理 main 时,它也处理了
etime = time.perf_counter()
print(f"Time Used: {etime - stime:0.4f}sec.\n")
打印 20 次。
求解中排除打印结果:
...
...
...
Time Used: 0.0001sec.
Time Used: 0.0001sec.
Time Used: 0.0000sec.
Time Used: 15.0268sec.
import time
import numpy as np
from mpmath import mp
import multiprocessing as mps
stime = time.perf_counter()
mp.dps = 50
accuracy = 6000000
R = 1
def solve(a):
n = a
theta = 360 / n
rad = mp.radians(theta/2)
print(n*R*mp.sin(rad))
if __name__ == "__main__":
pool = mps.Pool(mps.cpu_count())
pool.map(solve, range(1, accuracy))
pool.close()
pool.terminate()
pool.join()
etime = time.perf_counter()
print(f"Time Used: {etime - stime:0.4f}sec.\n")
【问题讨论】:
-
不,与我的主题无关。
标签: python numpy multiprocessing mpmath