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



我正在Windows机器上运行以下基准测试脚本。我注意到multiprocess((的执行顺序影响了它的性能。如果我先执行多进程,执行速度比简单&方法,如果我最后执行它,处理速度几乎是多线程((和简单方法的两倍。

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.84秒(
  • 结果#2:多线程(7.84s(->简单(7.53s(->多进程(13.96s(

为什么?如何在windows上正确使用多进程功能,以便通过使用CPU核心来提高速度

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

相关内容

  • 没有找到相关文章

最新更新