使用Python进行线程处理需要花费更长的时间,而不是使其速度更快? [英] Threading in Python takes longer time instead of making it faster?

查看:349
本文介绍了使用Python进行线程处理需要花费更长的时间,而不是使其速度更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了3种不同的代码来比较有线程与没有线程.基本上测量通过使用线程可以节省多少时间,结果没有任何意义.

I wrote 3 different codes to compare having threads vs. not having threads. Basically measuring how much time I save by using threading and the result didn't make any sense.

这是我的代码:

 import time



def Function():

    global x 
    x = 0

    while x < 300000000:
        x += 1
    print x

e1 = time.clock()
E1 = time.time()

Function() 

e2 = time.clock()
E2 = time.time()

print e2 - e1
print E2 - E1 

当我运行它时,我得到了它作为输出:

When I ran this, I got this as output:

26.6358742929

26.6440000534

然后我编写了另一个函数,如下所示,将最多3亿的计数分成3亿,即计数:

Then I wrote another function as shown below and split counting up to 300 million into counting 3, 100 millions:

 import time




def Function():

    global x 
    x = 0

    while x < 100000000:
        x += 1
    print x

def Function2():

    global x 
    x = 0

    while x < 100000000:
        x += 1
    print x       


def Function3():

    global x 
    x = 0

    while x < 100000000:
        x += 1
    print x 

e1 = time.clock()
E1 = time.time()

Function() 
Function2() 
Function3() 

e2 = time.clock()
E2 = time.time()

print e2 - e1
print E2 - E1   

以下函数的输出为:

26.0577638729

26.0629999638

最后,我创建了3个线程,并在单个线程上运行了每个函数:

and lastly I created 3 threads and ran each function on a single thread:

import time
import threading

e1 = time.clock()
E1 = time.time()

def Function1():

    global x 
    x = 0

    while  x < 100000000:
        x += 1
    print x


def Function2():

    global x 
    x = 0

    while x < 100000000:
        x += 1
    print x    


def Function3():

    global x 
    x = 0

    while x < 100000000:
        x += 1
    print x    



new_thread1  = threading.Thread(target = Function1() , args = ())

new_thread2  = threading.Thread(target = Function2(), args = ())

new_thread3  = threading.Thread(target = Function3(), args = ())


e1 = time.clock()
E1 = time.time()

new_thread1.start()
new_thread2.start()
new_thread3.start()

e2 = time.clock()
E2 = time.time()

print e2 - e1
print E2 - E1 

这一个的输出是:

0.000601416222253

0.0

这些数字对我来说毫无意义.我只是试图衡量线程节省了我多少时间.我在文档中查找并使用了time.timetime.clock对我来说很有意义,但在这里没有意义.另外,第一段和第二段的实际时间约为10秒,第三段的实际时间约为5

These numbers make no sense to me. I'm just trying to measure how much time does threading save me. I've looked up in the documentation and using time.time and time.clock made sense to me, but it doesn't make sense here. Also, the actual time for 1st and 2nd snippet were about 10 seconds and 3rd one about 5

推荐答案

您将其称为错误....

you are calling it wrong ....

 new_thread1  = threading.Thread(target = Function1 , args = ())

请注意,创建线程时不应调用该函数

note that you should not CALL the function when you create the thread

这些计时实际上没有任何意义,它们基本上都为零,因为您所计时的只是3个立即启动的即时返回函数调用

those timings really mean nothing they are both essentially zero because all you are timing is 3 instant return function calls to start

请注意,要获取输出,您将需要等待每个线程完成(因为您当前的代码未执行此操作)

note to get the output you will need to wait for each thread to finish (since your current code does not do this )

通过线程,您一次被gil锁定到一条python指令...这通常不是问题,因为您通常在磁盘io上等待...但是在示例代码中它是100%计算的,因此线程确实不会改善您的时间...多重处理可能如下所示

with threading you are locked by the gil to one python instruction at a time... typically this is not a problem since you are usually waiting on disk io... In your example code however it is 100% computation so threading really doesnt improve your time ... Multiprocessing may as demonstrated below

import time
import threading
import multiprocessing

def fn():
    '''since all 3 functions were identical you can just use one ...'''
    x = 0
    while  x < 100000000:
        x += 1




def TEST_THREADS():
    new_thread1  = threading.Thread(target = fn , args = ())
    new_thread2  = threading.Thread(target = fn, args = ())
    new_thread3  = threading.Thread(target = fn, args = ())
    new_thread1.start()
    new_thread2.start()
    new_thread3.start()
    new_thread1.join()
    new_thread2.join()
    new_thread3.join()

def TEST_NORMAL():
    fn()
    fn()
    fn()

def TEST_MULTIPROCESSING():
    new_thread1  = multiprocessing.Process(target = fn , args = ())
    new_thread2  = multiprocessing.Process(target = fn, args = ())
    new_thread3  = multiprocessing.Process(target = fn, args = ())
    new_thread1.start()
    new_thread2.start()
    new_thread3.start()
    new_thread1.join()
    new_thread2.join()
    new_thread3.join
if __name__ == "__main__":  
    '''It is very important to use name == __main__ guard code with threads and multiprocessing'''
    import timeit
    print "Time to Run 1x: %0.2fs"%(timeit.timeit(fn,number=1),)
    print "NORMAL:%0.2fs"%(timeit.timeit(TEST_NORMAL,number=1),)
    print "Threaded: %0.2fs"%(timeit.timeit(TEST_THREADS,number=1),)
    print "Multiprocessing: %0.2fs"%(timeit.timeit(TEST_MULTIPROCESSING,number=1),)

我得到以下输出

Time to Run 1x: 3.71181102665
NORMAL:11.0136830117
Threaded: 23.392143814
Multiprocessing: 3.80878260515

这篇关于使用Python进行线程处理需要花费更长的时间,而不是使其速度更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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