简体   繁体   English

1个工作线程比4个快?

[英]1 worker thread faster than 4?

I am reading myself into multighreading in python and came up with this simple test: (btw. this implementation might be very bad, I just wrote that down quickly for testing purpose. Buf if there is something terribly wrong I would be thankful if you could point that out) 我将自己读入python的multighreading中,并提出了一个简单的测试:(顺便说一句,此实现可能非常糟糕,我只是为了测试目的而将其写下来。如果有什么大错,我将很感激,如果您能指出)

#!/usr/bin/python2.7

import threading
import timeit

lst = range(0, 100000)
lstres = []
lstlock = threading.Lock()
lstreslock = threading.Lock()

def add_five(x):
    return x+5

def worker_thread(args):
    print "started"
    while len(lst) > 0:
        lstlock.acquire()
        try:
            x = lst.pop(0)
        except IndexError:
            lstlock.release()
            return
        lstlock.release()
        x = add_five(x)
        lstreslock.acquire()
        lstres.append(x)
        lstreslock.release()

def test():
    try:
        t1 = threading.Thread(target = worker_thread, args = (1,))
        #t2 = threading.Thread(target = worker_thread, args = (2,))
        #t3 = threading.Thread(target = worker_thread, args = (3,))
        #t4 = threading.Thread(target = worker_thread, args = (4,))
        t1.start();
        #t2.start();
        #t3.start();
        #t4.start();
        t1.join();
        #t2.join();
        #t3.join();
        #t4.join();
    except:
        print "Error"

    print len(lstres)

if __name__ == "__main__":
    t = timeit.Timer(test)
    print t.timeit(2)

Despite the terrible example I see the following: one thread is faster than 4. With one thread I get: 13.46 seconds, and with 4 threads: 25.47 seconds. 尽管有一个可怕的示例,但我看到以下内容:一个线程比4快。一个线程得到13.46秒,四个线程得到25.47秒。

Is the access to the list by 4 threads a bottleneck thus causing slower times or did I do something wrong? 通过4个线程访问列表是否是瓶颈,从而导致速度变慢或我做错了什么?

In your case, the Global Interpreter Lock isn't actually the problem. 在您的情况下, Global Interpreter Lock实际上不是问题。

Threading doesn't make things faster by default. 默认情况下,线程化不会使事情变得更快。 In your case, the code is CPU bound. 在您的情况下,代码受CPU限制。 No thread is ever waiting for I/O (which allow another to use the CPU). 没有线程正在等待I / O(这允许另一个人使用CPU)。 If you have code which needs 100% of the CPU, then threading will only make it faster if a lot of the code is independent which your's isn't: Most of your code is holding locks, so no other thread can proceed. 如果您的代码需要100%的CPU,那么只有在很多代码是独立的而不是您自己的代码的情况下,线程处理才能使其速度更快:大多数代码都持有锁,因此其他线程无法继续进行。

Which brings us to the cause of the slowdown: Switching threads and fighting for locks costs time. 这使我们陷入了放缓的原因:切换线程和争夺锁会浪费时间。 That's what eats 12s in your case. 在您的情况下,这就是吃12秒的东西。

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

相关问题 使用单个工作程序进行Python多处理比顺序操作更快 - Python multiprocessing with single worker faster than sequential operation python进程/线程映射在Windows中如何工作?为什么线程比进程更快? - How is python process/thread map working in windows?Why thread works faster than process? 工作线程中的网络摄像头 - Webcam from worker thread 如何与工作线程通信 - How to communicate with worker thread Python / Urllib2 / Threading:单个下载线程比多个下载线程快。 为什么? - Python/Urllib2/Threading: Single download thread faster than multiple download threads. Why? 为什么在 python 中下载数据集的非线程程序执行速度比线程程序快 - why non-thread program executing faster than threading program to download datasets in python Python 线程池在扫描 AWS S3 时比 Go 例程更快? - Python Thread Pool Faster than Go Routines when Scanning AWS S3? python2.7线程池比逐个启动线程快很多 - python2.7 threading pool is much faster than starting the thread one by one 在中等大小的 JSON 文件上使用线程池进行同步读取比异步读取更快 - Synchronous reading faster than asynchronous reading using thread pools on moderate-sized JSON files int> 0比!= 0快吗? - Is >0 faster than !=0 for an int?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM