为什么多进程产生不同的时序,因为执行顺序

时间:2018-11-13 23:50:40

标签: python multithreading multiprocessing

我正在Windows计算机上运行以下基准脚本。我注意到执行multiprocess()时的顺序会影响其性能。如果我先执行多进程,则执行速度要比simple&multithread()方法快,如果我最后执行它,则处理速度几乎是multithread()和simple方法的两倍。

import random
from threading import Thread
from multiprocessing import Process
import time
size = 10000000   # Number of random numbers to add to list
threads = 8 # Number of threads to create
my_list = []

for i in range(0,threads):
    my_list.append([])
def func(count, mylist):
    for i in range(count):
        mylist.append(random.random())

processes = []
for i in range(0, threads):
    p = Process(target=func,args=(size,my_list[i]))
    processes.append(p)


def multithreaded():
    jobs = []
    for i in range(0, threads):
        thread = Thread(target=func,args=(size,my_list[i]))
        jobs.append(thread)
    # Start the threads
    for j in jobs:
        j.start() 
    # Ensure all of the threads have finished
    for j in jobs:
        j.join()

def simple():
    for i in range(0, threads):
        func(size,my_list[i])

def multiprocessed():
    global processes
    # Start the processes
    for p in processes:
        p.start()
    # Ensure all processes have finished execution
    for p in processes:
        p.join()
if __name__ == "__main__":
    start = time.time()
    multiprocessed()
    print("elasped time:{}".format(time.time()-start))
    start = time.time()
    simple()
    print("elasped time:{}".format(time.time()-start))
    start = time.time()
    multithreaded()
    print("elasped time:{}".format(time.time()-start))
  • 结果#1:多进程(2.85s)->简单(7.39s)->多线程 (7.84s)
  • 结果#2:多线程(7.84s)->简单(7.53s)-> 多处理(13.96 s)

那是为什么?如何在Windows上正确使用多进程功能,以通过利用CPU内核来提高速度

1 个答案:

答案 0 :(得分:3)

您的计时代码不会将每个测试与其他测试隔离开来。如果首先执行multiprocessed,则my_list的子列表为空。如果最后执行它,则子列表将充满其他运行添加的元素,从而大大增加了将数据发送到工作进程所涉及的通信开销。