简体   繁体   English

Python多处理速度与单个进程

[英]Python Multiprocessing speed with single process

I've found some behavior with python multiprocessing I'm having difficulties understanding. 我发现python多处理的一些行为我很难理解。 When using Pool, even if it is a single process, it performs much, much faster. 使用Pool时,即使它是单个进程,它也会执行更快,更快的速度。

Why is that? 这是为什么? Does multiprocessing somehow optimize the code? 多处理是否以某种方式优化代码?

import time
from multiprocessing import Pool


fib_input = [24] * 10

def fib(n):
    if n in [0,1]:
        return 1
    return fib(n-1)+fib(n-2)


def main1():
    with Pool(processes=1) as p:
        results = p.map(fib, fib_input)
    print (results)


def main2():
    results = list(map(fib, fib_input))
    print (results)


if __name__ == "__main__":
    start_time = time.time()
    main1()
    print("--- %s seconds ---" % (time.time() - start_time))

    start_time = time.time()
    main2()
    print("--- %s seconds ---" % (time.time() - start_time))

Output: 输出:

[75025, 75025, 75025, 75025, 75025, 75025, 75025, 75025, 75025, 75025]
--- 0.47702741622924805 seconds ---
[75025, 75025, 75025, 75025, 75025, 75025, 75025, 75025, 75025, 75025]
--- 7.922452926635742 seconds ---

Ok. 好。 Thanks to comments I figured out my bad. 感谢评论,我发现了我的不好。 Thanks guys! 多谢你们!

A rookie mistake. 一个菜鸟的错误。

I was using Visual Studio PTVS. 我使用的是Visual Studio PTVS。 That is where the slowdown was from. 这就是经济放缓的原因。 I've changed the dropdown 'build' to Release, however pressing f5 was still running debug mode, while I was convinced it was a clean run. 我已经将下拉列表'build'更改为Release,但是按下f5仍然在运行调试模式,而我确信这是一个干净的运行。

Running it in cmd outside did the trick. 在外面的cmd中运行它就可以了。 Later I figured that also ctrl-f5 starts without debugging. 后来我发现ctrl-f5也开始没有调试。

Thanks for the help. 谢谢您的帮助。

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

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