繁体   English   中英

为什么在此python代码中,单个进程池比序列化实现快?

[英]Why is a single process pool faster than serialized implementation in this python code?

我在python中遇到多重处理。 我知道它比序列化计算要慢,这不是我的帖子要讲的。

我只是在徘徊,为什么一个进程池比我的基本问题的序列化计算要快。 这些时间不应该一样吗?

这是代码:

import time
import multiprocessing as mp
import matplotlib.pyplot as plt


def func(x):
    return x*x*x


def multi_proc(nb_procs):
    tic = time.time()
    pool = mp.Pool(processes=nb_procs)
    pool.map_async(func, range(1, 10000000))
    toc = time.time()
    return toc-tic


def single_core():
    tic = time.time()
    [func(x) for x in range(1, 10000000)]
    toc = time.time()
    return toc-tic

if __name__ == '__main__':
    sc_times = [0]
    mc_times = [0]
    print('single core computation')
    sc_constant_time = single_core()
    print('{} secs'.format(sc_constant_time))
    for nb_procs in range(1, 12):
        print('computing for {} processes...'.format(nb_procs))
        time_elapsed = (multi_proc(nb_procs))
        print('{} secs'.format(time_elapsed))
        mc_times.append(time_elapsed)
    sc_times = [sc_constant_time for _ in mc_times]
    plt.plot(sc_times, 'r--')
    plt.plot(mc_times, 'b--')
    plt.xlabel('nb procs')
    plt.ylabel('time (s)')
    plt.show()

以及每进程数的时间图(红色=串行计算,蓝色=多处理): 在此处输入图片说明

编辑1:如Sidhnarth Gupta所示,我修改了代码,这是我拥有的新代码。 我无故更改了功能。

import time
import multiprocessing as mp
import matplotlib.pyplot as plt
import random


def func(x):
    return random.choice(['a', 'b', 'c', 'd', 'e', 'f', 'g'])


def multi_proc(nb_procs, nb_iter):
    tic = time.time()
    pool = mp.Pool(processes=nb_procs)
    pool.map_async(func, range(1, nb_iter)).get()
    toc = time.time()
    return toc-tic


def single_core(nb_iter):
    tic = time.time()
    [func(x) for x in range(1, nb_iter)]
    toc = time.time()
    return toc-tic

if __name__ == '__main__':
    # configure
    nb_iter = 100000
    max_procs = 16
    sc_times = [0]
    mc_times = [0]

    # multi proc calls
    for nb_procs in range(1, max_procs):
        print('computing for {} processes...'.format(nb_procs))
        time_elapsed = (multi_proc(nb_procs, nb_iter))
        print('{} secs'.format(time_elapsed))
        mc_times.append(time_elapsed)

    # single proc call
    print('single core computation')
    for nb in range(1, len(mc_times)):
        print('{}...'.format(nb))
        sc_times.append(single_core(nb_iter))
    # average time
    average_time = sum(sc_times)/len(sc_times)
    print('average time on single core: {} secs'.format(average_time))

    # plot
    plt.plot(sc_times, 'r--')
    plt.plot(mc_times, 'b--')
    plt.xlabel('nb procs')
    plt.ylabel('time (s)')
    plt.show()

这是我的新情节:

在此处输入图片说明

我想我现在可以说我通过使用多处理提高了程序的速度。

您当前用于计算多处理时间的代码实际上是在告知将任务提交到池所需的时间。 处理实际上是在异步模式下进行的,没有阻塞线程。

我通过以下更改尝试了您的程序:

def multi_proc(nb_procs):
    tic = time.time()
    pool = mp.Pool(processes=nb_procs)
    pool.map_async(func, range(1, 10000000)).get()
    toc = time.time()
    return toc-tic

def multi_proc(nb_procs):
    tic = time.time()
    pool = mp.Pool(processes=nb_procs)
    pool.map(func, range(1, 10000000))
    toc = time.time()
    return toc-tic

与序列化计算相比,它们花费的时间明显更多。

同样,在创建此类图时,您还应该考虑在每次要映射值时都调用single_core()函数,而不是多次映射同一值。 您将看到同一时间所花费的时间差异很大。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM