进程的多处理不会减少运行时(Python)



我正在使用随机方法来近似d维球体的体积。我首先使用n=10^6的样本量作为一个单独的过程。然后,我试着用n=10^5的样本量作为10个平行过程来开始同样的近似。

由于函数是ordo(N(,我假设执行时间会减少约10倍,但事实并非如此。有什么想法吗?

import n_sphere
# import multiprocessing as mp
from time import perf_counter as pc
import concurrent.futures as future
from time import sleep as wait
import math
from numpy import mean, round

def pause(sec):
wait(sec)

# ====== # Parameters for assignment # ====== #
n = 10 ** 6
d = 11
r = 1
# ============================================ #
#
#
# ==== # Parameters for multiprocessing # ==== #
thread = 10
p1 = [int(n / thread) for i1 in range(thread)]
p2 = [d for i2 in range(thread)]
p3 = [r for i3 in range(thread)]
# ============================================ #
#
#
# =========== Time for non-mp ================ #
t1 = pc()
volume_non_mp = n_sphere.N_sphere(n, d, r)
t2 = pc()
# ============================================ #
#
#
# =========== Time for mp ==================== #
t3 = pc()
with future.ThreadPoolExecutor() as ex:
volume_mp = mean(list(ex.map(n_sphere.N_sphere, p1, p2, p3)))
t4 = pc()
# ============================================ #
#
#
# =========== Displaying results ============= #
print(f'''
Time w/o multiprocessing: {round(volume_non_mp, 4)}               time: {round(t2 - t1, 4)}s
Time w/ multiprocessing:  {round(volume_mp, 4)}               time: {round(t4 - t3, 4)}s''')
# ============================================ #
#
#
# =========== Displaying results ============= #
v_d = math.pi ** (d / 2) * (r ** d) / (math.gamma((d / 2 + 1)))
print(f'nActual volume:  {v_d}')
# ============================================ #

N_sphere函数如下所示:

import random
import math
import functools

def N_sphere(nf, df, rf):
# Producing 'n' cords of dimension 'd' within a radius of 'r'
cord_list = [[random.uniform(-rf, rf) for i in range(df)] for cords in range(nf)]
squares = []
for i in range(nf):
squares.append(functools.reduce(lambda x, y: math.sqrt(x*x + y*y), cord_list[i]))
n_in = list(filter(lambda x: x <= rf, squares))
n_out = list(filter(lambda x: x > rf, squares))
volume = ((2 * rf) ** df) * (len(n_in) / (len(n_out) + len(n_in)))
return volume

Python GIL不允许使用多个CPU内核并行运行线程。它可以同时运行线程。尽管看起来线程是同时运行的,但在场景背后,一个CPU内核在时间片中连续运行它们。因此,多线程不会减少Python中的执行时间是合乎逻辑的(它在I/O绑定的情况下会减少执行时间,但在您的情况下不会(。

您可以阅读本文以获取更多信息。

除了这些技术问题之外,您在代码中使用的是multithreading,而不是多处理。要进行多进程,请使用concurrent。ProcessPoolExecute而不是ThreadPoolExecute,它应该会减少时间。

最新更新