多线程性能开销 [英] Multithreading performance overhead

查看:77
本文介绍了多线程性能开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,基本上,我创建了这个程序,该程序向Redis添加值.到目前为止,我得到了这个时机:

So basically I created this program that adds values to redis. So far I get this timing:

real 0m27.759s
user 0m18.129s
sys  0m5.580s

但是,当我尝试运行多个线程时:

However when I tried to run multiple threads:

if __name__ == '__main__':
    try:
        for x in range(0, NUM_THREADS):
            Thread(None, startProgram, None,
                   (NUM_HOSTS/NUM_THREADS*x+1,
                    NUM_HOSTS/NUM_THREADS*(x+1))).start()
    except Exception as errtxt:
        print errtxt

我通过NUM_THREADS设置为ot 10来获得此信息:

I get this with NUM_THREADS set ot 10:

real 0m32.642s
user 0m22.953s
sys  0m11.473s

为什么我的程序运行的 slower 带有 more 个线程?

Why is my program running slower with more threads?

我正在运行Linux Ubuntu 11.04和Python 2.7.1.

I'm running Linux Ubuntu 11.04 and Python 2.7.1.

推荐答案

结果取决于Python实现,即cpython的 GIL 可防止并行计算比顺序计算更快.

The result depends on the Python implementation, cpython's GIL prevents parallel computations from being faster than sequential ones.

考虑使用 multiprocessing 模块,该模块在自己的Python中执行每个线程进程或其他无GIL的Python实现,例如 IronPython 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆