Python并行性不工作

2024-09-29 02:20:49 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:我在评论中得到了很好的反馈。对代码做了一些更改,但我仍然看到类似的问题。现在看来我的计时方式有点问题,因为第二次操作需要超过0秒。你知道吗

原件: 我写的代码表面上是并行的,但实际上并没有运行得更快——并行和非并行版本都需要相同的时间。我一辈子都搞不懂为什么。你知道吗

我正在Windows7上通过Anaconda使用Python3.4。无论我是在IDE(Spyder)还是在命令提示符下提交作业,结果都是一样的。这是我的密码:

import multiprocessing
from multiprocessing import Pool
import time

def divide_by_two(n):
    time.sleep(.1)
    return n/2

if __name__ == '__main__':
    print("The number of cores is ", multiprocessing.cpu_count())
    pool = Pool(processes=multiprocessing.cpu_count())
    smallList = range(0,1000) #[0, 2, 4, 6, 8, 10]
    print('Checking the parallelized way', smallList[:5])
    start = time.time()
    result = pool.map(divide_by_two, smallList)
    end = time.time()
    cleaned = [x for x in result if not x is None]
    print('small List divided by two is ', str(cleaned[:5]))
    print('Parallel way takes ', str(end-start), ' seconds')

    #Now the dumb version
    print('Checking the slow way', smallList[:5])
    start2 = time.time()
    smallList = range(0,1000) #[0, 2, 4, 6, 8, 10]
    result2 = map(divide_by_two, smallList)
    end2 = time.time()
    cleaned2 = [x for x in result2 if not x is None]
    print('small List divided by two is ', str(cleaned2[:5]))
    print('The slow way takes', str(end2-start2), ' seconds')

以下是输出:

The number of cores is  4
Checking the parallelized way range(0, 5)
small List divided by two is  [0.0, 0.5, 1.0, 1.5, 2.0]
Parallel way takes  26.87681818008423  seconds
Checking the slow way range(0, 5)
small List divided by two is  [0.0, 0.5, 1.0, 1.5, 2.0]
The slow way takes 0.0  seconds

Tags: thebytimeisrangemultiprocessingwaylist
1条回答
网友
1楼 · 发布于 2024-09-29 02:20:49

感谢评论。如果可以的话我会投票给你的。增加了并行化问题的规模,这次使用了正确的变量(哦!)使用不同的计时功能。在这里:

import multiprocessing
from multiprocessing import Pool
import time

def divide_by_two(n):
    time.sleep(.1)
    return n/2

if __name__ == '__main__':
    print("The number of cores is ", multiprocessing.cpu_count())
    pool = Pool(processes=multiprocessing.cpu_count())
    smallList = range(0,1000) #[0, 2, 4, 6, 8, 10]
    print('Checking the parallelized way', smallList[:5])
    start = time.clock() #time.time()
    print('Start 1 is ', start)
    result = pool.map(divide_by_two, smallList)
    end = time.time()
    cleaned = [x for x in result if not x is None]
    print('small List divided by two is ', str(cleaned[:5]))
    print('Parallel way takes ', time.clock()-start, ' seconds')    

    #Now the dumb version
    print('Checking the slow way', smallList[:5])
    start = time.clock()
    print('Start 2 is ', start)
    result2 = map(divide_by_two, smallList)
    cleaned2 = [x for x in result2 if not x is None]
    print('small List divided by two is ', str(cleaned2[:5]))
    print('The slow way takes', time.clock()-start, ' seconds')

相关问题 更多 >