简体   繁体   English

使用Python进行线程处理需要花费更长的时间,而不是使其速度更快?

[英]Threading in Python takes longer time instead of making it faster?

I wrote 3 different codes to compare having threads vs. not having threads. 我编写了3个不同的代码来比较有线程与没有线程。 Basically measuring how much time I save by using threading and the result didn't make any sense. 基本上测量使用线程节省了多少时间,结果没有任何意义。

Here are my codes: 这是我的代码:

 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

Then I wrote another function as shown below and split counting up to 300 million into counting 3, 100 millions: 然后,我编写了另一个函数,如下所示,将最多3亿的计数分成3,1亿:

 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   

The output of the following function was: 以下函数的输出为:

26.0577638729

26.0629999638

and lastly I created 3 threads and ran each function on a single thread: 最后,我创建了3个线程,并在单个线程上运行了每个函数:

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 

The out put of this one was: 该产品的输出是:

0.000601416222253

0.0

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. 我在文档中查找并使用time.timetime.clock对我来说很有意义,但是在这里没有意义。 Also, the actual time for 1st and 2nd snippet were about 10 seconds and 3rd one about 5 另外,第一段和第二段的实际时间约为10秒,第三段的实际时间约为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 请注意,创建线程时不应调用该函数

those timings really mean nothing they are both essentially zero because all you are timing is 3 instant return function calls to start 这些计时实际上并不意味着它们基本上都为零,因为您所计时的只是3次即时返回函数调用以启动

note to get the output you will need to wait for each thread to finish (since your current code does not do this ) 注意要获取输出,您将需要等待每个线程完成(因为您当前的代码不执行此操作)

EDIT FOR MORE INFO 编辑更多信息

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

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),)

I get the following output 我得到以下输出

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

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

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