Python如何給你的程序做性能測試
問題
你想測試你的程序運行所花費的時間并做性能測試。
解決方案
如果你只是簡單的想測試下你的程序整體花費的時間, 通常使用Unix時間函數(shù)就行了,比如:
bash % time python3 someprogram.py real 0m13.937s user 0m12.162s sys 0m0.098s bash %
如果你還需要一個程序各個細節(jié)的詳細報告,可以使用 cProfile 模塊:
bash % python3 -m cProfile someprogram.py
859647 function calls in 16.016 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
263169 0.080 0.000 0.080 0.000 someprogram.py:16(frange)
513 0.001 0.000 0.002 0.000 someprogram.py:30(generate_mandel)
262656 0.194 0.000 15.295 0.000 someprogram.py:32(<genexpr>)
1 0.036 0.036 16.077 16.077 someprogram.py:4(<module>)
262144 15.021 0.000 15.021 0.000 someprogram.py:4(in_mandelbrot)
1 0.000 0.000 0.000 0.000 os.py:746(urandom)
1 0.000 0.000 0.000 0.000 png.py:1056(_readable)
1 0.000 0.000 0.000 0.000 png.py:1073(Reader)
1 0.227 0.227 0.438 0.438 png.py:163(<module>)
512 0.010 0.000 0.010 0.000 png.py:200(group)
...
bash %
不過通常情況是介于這兩個極端之間。比如你已經(jīng)知道代碼運行時在少數(shù)幾個函數(shù)中花費了絕大部分時間。 對于這些函數(shù)的性能測試,可以使用一個簡單的裝飾器:
# timethis.py
import time
from functools import wraps
def timethis(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.perf_counter()
r = func(*args, **kwargs)
end = time.perf_counter()
print('{}.{} : {}'.format(func.__module__, func.__name__, end - start))
return r
return wrapper
要使用這個裝飾器,只需要將其放置在你要進行性能測試的函數(shù)定義前即可,比如:
>>> @timethis ... def countdown(n): ... while n > 0: ... n -= 1 ... >>> countdown(10000000) __main__.countdown : 0.803001880645752 >>>
要測試某個代碼塊運行時間,你可以定義一個上下文管理器,例如:
from contextlib import contextmanager
@contextmanager
def timeblock(label):
start = time.perf_counter()
try:
yield
finally:
end = time.perf_counter()
print('{} : {}'.format(label, end - start))
下面是使用這個上下文管理器的例子:
>>> with timeblock('counting'):
... n = 10000000
... while n > 0:
... n -= 1
...
counting : 1.5551159381866455
>>>
對于測試很小的代碼片段運行性能,使用 timeit 模塊會很方便,例如:
>>> from timeit import timeit
>>> timeit('math.sqrt(2)', 'import math')
0.1432319980012835
>>> timeit('sqrt(2)', 'from math import sqrt')
0.10836604500218527
>>>
timeit 會執(zhí)行第一個參數(shù)中語句100萬次并計算運行時間。 第二個參數(shù)是運行測試之前配置環(huán)境。如果你想改變循環(huán)執(zhí)行次數(shù), 可以像下面這樣設置 number 參數(shù)的值:
>>> timeit('math.sqrt(2)', 'import math', number=10000000)
1.434852126003534
>>> timeit('sqrt(2)', 'from math import sqrt', number=10000000)
1.0270336690009572
>>>
討論
當執(zhí)行性能測試的時候,需要注意的是你獲取的結果都是近似值。 time.perf_counter() 函數(shù)會在給定平臺上獲取最高精度的計時值。 不過,它仍然還是基于時鐘時間,很多因素會影響到它的精確度,比如機器負載。 如果你對于執(zhí)行時間更感興趣,使用 time.process_time() 來代替它。例如:
from functools import wraps
def timethis(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.process_time()
r = func(*args, **kwargs)
end = time.process_time()
print('{}.{} : {}'.format(func.__module__, func.__name__, end - start))
return r
return wrapper
最后,如果你想進行更深入的性能分析,那么你需要詳細閱讀 time 、timeit 和其他相關模塊的文檔。 這樣你可以理解和平臺相關的差異以及一些其他陷阱。 還可以參考13.13小節(jié)中相關的一個創(chuàng)建計時器類的例子。
以上就是Python如何給你的程序做性能測試的詳細內容,更多關于Python做性能測試的資料請關注腳本之家其它相關文章!
相關文章
Python辦公自動化之數(shù)據(jù)可視化與報表生成
在現(xiàn)代辦公環(huán)境中,數(shù)據(jù)處理和報表生成是一項重要的任務,本文將高效介紹如何使用Python進行數(shù)據(jù)可視化和報表生成,讓您的辦公工作更加順利2023-07-07
基于Tensorflow搭建一個神經(jīng)網(wǎng)絡的實現(xiàn)
神經(jīng)網(wǎng)絡可能會讓人感到恐懼,特別是對于新手機器學習的人來說。這篇文章主要介紹了基于Tensorflow搭建一個神經(jīng)網(wǎng)絡的實現(xiàn),從入門開始,感興趣的可以了解一下2021-05-05
python將logging模塊封裝成單獨模塊并實現(xiàn)動態(tài)切換Level方式
這篇文章主要介紹了python將logging模塊封裝成單獨模塊并實現(xiàn)動態(tài)切換Level方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
如何使用python數(shù)據(jù)處理解決數(shù)據(jù)沖突和樣本的選取
這篇文章主要介紹了如何使用python數(shù)據(jù)處理解決數(shù)據(jù)沖突和樣本的選取,其中主要包括 實際業(yè)務數(shù)據(jù)沖突、樣本選取問題、數(shù)據(jù)共線性等思路2021-08-08
詳解pycharm2020.1.1專業(yè)版安裝指南(推薦)
這篇文章主要介紹了pycharm2020.1.1專業(yè)版安裝指南,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08

