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

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

我写了 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 对我来说有意义,但在这里没有意义。此外,第一个和第二个片段的实际时间约为 10 秒,第三个片段的实际时间约为 5

你叫错了....

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

注意创建线程时不要调用函数

这些计时真的没有任何意义,它们本质上都是零,因为你所计时的只是 3 个即时 return 函数调用以开始

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

编辑以获取更多信息

使用线程时,你被 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),)

我得到以下输出

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