我试图了解如何使Cython运行并行循环。下面的代码确实以大约相同的速度运行,系统监视器在任何时间点只显示一个核心负载。 Wham我做错了吗?
我正在使用Ipython Notebook来运行它。
from cython.parallel import prange, parallel, threadid
cdef long i,j
cdef long total,subtotal = 0
cdef long n = 10**10
for i in range(n):
total += i
CPU times: user 9.02 s, sys: 0 ns, total: 9.02 s
Wall time: 9.03 s
with nogil, parallel():
for i in prange(8):
subtotal = 0
for j in range(i,n,8):
subtotal += i
total += subtotal
CPU times: user 13.1 s, sys: 28 ms, total: 13.1 s
Wall time: 13.4 s