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

Python中的线程化需要更长的时间,而不是更快?,python,multithreading,time,python-multithreading,python-multiprocessing,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.

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

 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   
最后,我创建了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 
这些数字对我来说毫无意义。我只是想测量线程化能节省我多少时间。我查阅了文档,并使用
time.time

还有
time.clock
对我来说是有意义的,但在这里没有意义。另外,第1段和第2段的实际时间大约是10秒,第3段大约是5秒。你说的不对

 new_thread1  = threading.Thread(target = Function1 , args = ())
请注意,创建线程时不应调用该函数

这些计时实际上毫无意义,它们本质上都是零,因为所有计时都是3个即时返回函数调用开始

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

编辑以获取更多信息 使用线程,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/O阻塞时执行其他工作;它们只受CPU时间的限制,对于单个进程,线程管理所花费的时间只是实际计算所花费的时间。@chepner我如何使其更快?您需要使用多个进程,所以每个函数都在一个单独的CPU上的进程中运行。@chepner所以我必须使用多进程而不是线程?!我以为线程可以解决这个问题。如果我有非计算性的工作,线程会加速我的代码吗?对不起,有多个问题。这里的编程新手。@joran_beasley其他事情看起来都对吗?!现在我得到了0.0523433269691 0.0499999523163,代码速度明显加快。但是另一方面,第一和第二个代码没有花26秒。你没有一大堆问题…在代码的第一行启动计时器。。。并在所有线程重新连接后停止它(使用threadX.join()等待线程完成)@joran_beasley,与new_thread1.start()new_thread1.join()new_thread2.start()new_thread2.join()new_thread3.start()不启动所有线程。。。然后加入他们所有人。。。join将阻止程序,直到线程returns@joran_beasley谢谢你的帮助。这样行吗?
0.000601416222253

0.0
 new_thread1  = threading.Thread(target = Function1 , args = ())
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