Ope*_*nCv 4 python time multithreading 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
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我得到以下输出:
26.6358742929
26.6440000534
Run Code Online (Sandbox Code Playgroud)
然后我编写了另一个函数,如下所示,将计数到 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
Run Code Online (Sandbox Code Playgroud)
以下函数的输出是:
26.0577638729
26.0629999638
Run Code Online (Sandbox Code Playgroud)
最后我创建了 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
Run Code Online (Sandbox Code Playgroud)
这个的输出是:
0.000601416222253
0.0
Run Code Online (Sandbox Code Playgroud)
这些数字对我来说毫无意义。我只是想测量线程为我节省了多少时间。我查阅了文档并使用了 time.time
它time.clock,这对我来说很有意义,但在这里没有意义。此外,第一个和第二个片段的实际时间约为 10 秒,第三个片段的实际时间约为 5 秒
你叫错了......
new_thread1 = threading.Thread(target = Function1 , args = ())
Run Code Online (Sandbox Code Playgroud)
请注意,创建线程时不应调用该函数
这些计时实际上没有任何意义,它们本质上都是零,因为您所计时的只是 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),)
Run Code Online (Sandbox Code Playgroud)
我得到以下输出
Time to Run 1x: 3.71181102665
NORMAL:11.0136830117
Threaded: 23.392143814
Multiprocessing: 3.80878260515
Run Code Online (Sandbox Code Playgroud)