哪些模組可用於python效能分析 基準和效能分析

2021-10-11 03:24:03 字數 4315 閱讀 6691

在本章中,我們將學習基準測試和分析如何幫助解決效能問題。

假設我們已經編寫了乙個**,並且它也給出了期望的結果,但是如果想要更快地執行此**,因為需求已經發生了變化。 在這種情況下,需要找出**的哪些部分正在減慢整個程式。 在這種情況下,基準測試和分析可能很有用。

基準測試是什麼?

基準測試旨在通過與標準進行比較來評估某些事物。 然而,這裡出現的問題是,什麼是基準,以及為什麼需要軟體程式設計。 對**進行基準測試意味著**的執行速度以及瓶頸的位置。 基準測試的乙個主要原因是它優化了**。

基準是如何工作?

如果我們談論基準測試的工作,需要首先將整個程式作為乙個當前狀態,然後可以將微基準結合起來,然後將程式分解成更小的程式。 找到程式中的瓶頸並優化它。 換句話說,我們可以把它理解為將大而難的問題分解為一系列較小和較容易的問題來優化它們。

python模組進行基準測試

在python中,我們有乙個預設的基準測試模組,稱為timeit。 在timeit模組的幫助下,我們可以在主程式中測量一小段python**的效能。

示例在下面的python指令碼中,匯入了timeit模組,它進一步測量執行兩個函式所需的時間 - functiona和functionb -

import timeit

import time

def functiona():

print("function a starts the execution:")

print("function a completes the execution:")

def functionb():

print("function b starts the execution")

print("function b completes the execution")

start_time = timeit.default_timer()

functiona()

print(timeit.default_timer() - start_time)

start_time = timeit.default_timer()

functionb()

print(timeit.default_timer() - start_time)

執行上面的指令碼之後,將得到兩個函式的執行用時,如下所示。

function a starts the execution:

function a completes the execution:

0.0014599495514175942

function b starts the execution

function b completes the execution

0.0017024724827479076

使用裝飾器函式編寫計時器

在python中,我們可以建立自己的計時器,它的行為就像timeit模組一樣。 它可以在裝飾器功能的幫助下完成。 以下是自定義計時器的示例 -

import random

import time

def timer_func(func):

a timer decorator

def function_timer(*args, **kwargs):

a nested function for timing other functions

start = time.time()

value = func(*args, **kwargs)

end = time.time()

runtime = end - start

msg = " took seconds to complete its execution."

print(msg.format(func = func.__name__,time = runtime))

return value

return function_timer

@timer_func

def myfunction():

for x in range(5):

sleep_time = random.choice(range(1,3))

time.sleep(sleep_time)

if __name__ == '__main__':

myfunction()

上面的python指令碼有助於匯入隨機時間模組。 我們建立了timer_func()裝飾器函式。 這裡面有function_timer()函式。 現在,巢狀函式會在呼叫傳入函式之前抓取時間。 然後它等待函式返回並抓取結束時間。 這樣,我們可以最終使python指令碼列印執行時間。 該指令碼將生成如下所示的輸出。

myfunction took 8.000457763671875 seconds to complete its execution.

什麼是效能分析?

有時程式設計師想要測量一些屬性,如使用記憶體,時間複雜度或使用關於程式的特定指令來衡量程式的真實能力。 這種關於程式的測量稱為分析。 分析使用動態程式分析來進行這種測量。

在隨後的章節中,我們將學習用於分析的不同python模組。

cprofile - 內建模組

cprofile是乙個用於分析的python內建模組。 該模組是乙個具有合理開銷的c擴充套件,適合分析長時間執行的程式。 執行後,它會記錄所有的功能和執行時間。 這是非常強大的,但有時難以解釋和操作。 在下面的例子中,我們在下面的**中使用cprofile -

示例def increment_global():

global x

x += 1

def taskofthread(lock):

for _ in range(50000):

lock.acquire()

increment_global()

lock.release()

def main():

global x

x = 0

lock = threading.lock()

t1 = threading.thread(target=taskofthread, args=(lock,))

t2 = threading.thread(target= taskofthread, args=(lock,))

t1.start()

t2.start()

t1.join()

t2.join()

if __name__ == "__main__":

for i in range(5):

main()

print("x = after iteration ".format(i,x))

上面的**儲存在thread_increment.py檔案中。 現在,在命令列上用cprofile執行**如下 -

(base) d:\programdata>python -m cprofile thread_increment.py

x = 100000 after iteration 0

x = 100000 after iteration 1

x = 100000 after iteration 2

x = 100000 after iteration 3

x = 100000 after iteration 4

3577 function calls (3522 primitive calls) in 1.688 seconds

ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)

5 0.000 0.000 0.000 0.000 :103(release)

5 0.000 0.000 0.000 0.000 :143(__init__)

5 0.000 0.000 0.000 0.000 :147(__enter__)

從上面的輸出中可以清楚地看到,cprofile列印出所有被呼叫的3577個函式,每個函式花費的時間和呼叫的次數。 以下是我們在輸出中獲得的列 -

ncalls - 這是要呼叫的數字值。

tottime - 這是在給定函式中花費的總時間。

percall - 它指的是tottime除以ncalls的商。

cumtime - 這是在這個和所有子功能中累計的時間。 遞迴函式甚至是準確的。

percall - 它是cumtime除以原始呼叫的商。

filename:lineno(function) - 它基本上提供了每個函式的相應資料。

¥ 我要打賞

糾錯/補充

收藏加qq群啦,易百教程官方技術學習群

注意:建議每個人選自己的技術方向**,同乙個qq最多限加 3 個群。

PHP手冊 declare 可用於PHP效能測試

function profile 註冊tick方法 register tick function profile 設定每執行幾條語句執行已註冊的方法這裡設定了3條 每次 declare ticks 3 輸出結果 now tmp is 3.n now tmp is 6.n now tmp is 8.n...

python模組之模組用於定義

任何python程式都可以作為模組匯入 通過python idle執行儲存在資料夾中的檔案 這裡是告訴編譯器,除了從預設的目錄中查詢外,還要從d program files x86 python3.5中查詢模組 生成的.pyc檔案是 平台無關的 經過處理 編譯 的,已經轉換成python能夠更加處理...

python需要安裝哪些模組 Python安裝模組

1.python安裝模組最簡單的方式就是直接在命令列中輸入 pip install 模組名。1.2 如果使用的是低版本的pip安裝某些模組時可能會報錯,此時需要把pip公升級為最新版本,公升級也很簡單只需要在命令列中輸入 pip install upgrade pip 即可 也需要聯網喲!安裝說明 ...