Python中的线程需要更长的时间而不是更快?

时间:2015-04-08 19:10:22

标签: python multithreading time python-multithreading python-multiprocessing

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

以下是我的代码:

 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 

当我跑这个时,我把它作为输出:

26.6358742929

26.6440000534

然后我写下了另一个函数,如下所示,将计数分为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   

以下功能的输出是:

26.0577638729

26.0629999638

最后我创建了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 

这个的输出是:

0.000601416222253

0.0

这些数字对我没有意义。我只想测量线程节省多少时间。我查阅了文档并使用time.time time.clock对我有意义,但这里没有意义。此外,第1和第2片段的实际时间约为10秒,第3片约为5秒

1 个答案:

答案 0 :(得分:2)

你说错了......

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

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

那些时间真的没有任何意义,它们都基本上都是零,因为你所有的时间都是3个即时返回函数调用开始

注意要获得输出,您需要等待每个线程完成(因为您当前的代码不会这样做)

编辑更多信息

使用线程你一次被gil锁定到一条python指令...通常这不是问题,因为你经常在磁盘上等待...在你的示例代码中它是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),)

我得到以下输出

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