以執行時間為重點的程式碼分析

撰寫高效的 Python 程式碼

Logan Thomas

Scientific Software Technical Trainer, Enthought

程式碼分析

  • 函式呼叫次數與耗時的詳細統計
  • 逐行分析
  • 使用套件:line_profiler
pip install line_profiler
撰寫高效的 Python 程式碼

程式碼分析:執行時間

heroes = ['Batman', 'Superman', 'Wonder Woman']

hts = np.array([188.0, 191.0, 183.0])

wts = np.array([ 95.0, 101.0,  74.0])
撰寫高效的 Python 程式碼
def convert_units(heroes, heights, weights):

    new_hts = [ht * 0.39370  for ht in heights]
    new_wts = [wt * 2.20462  for wt in weights]

    hero_data = {}

    for i,hero in enumerate(heroes):
        hero_data[hero] = (new_hts[i], new_wts[i])

    return hero_data
convert_units(heroes, hts, wts)
{'Batman': (74.0156, 209.4389),
 'Superman': (75.1967, 222.6666),
 'Wonder Woman': (72.0471, 163.1419)}
撰寫高效的 Python 程式碼

程式碼分析:執行時間

%timeit convert_units(heroes, hts, wts)
3 µs ± 32 ns 每回合(7 次執行的平均值 ± 標準差,每次 100000 回合)
撰寫高效的 Python 程式碼
%timeit new_hts = [ht * 0.39370  for ht in hts]
1.09 µs ± 11 ns 每回合(7 次執行的平均值 ± 標準差,每次 1000000 回合)
%timeit new_wts = [wt * 2.20462  for wt in wts]
1.08 µs ± 6.42 ns 每回合(7 次執行的平均值 ± 標準差,每次 1000000 回合)
%%timeit
hero_data = {}
for i,hero in enumerate(heroes):
    hero_data[hero] = (new_hts[i], new_wts[i])
634 ns ± 9.29 ns 每回合(7 次執行的平均值 ± 標準差,每次 1000000 回合)
撰寫高效的 Python 程式碼

程式碼分析:line_profiler

使用 line_profiler 套件

%load_ext line_profiler

逐行計時的魔術命令

%lprun -f
撰寫高效的 Python 程式碼

程式碼分析:line_profiler

使用 line_profiler 套件

%load_ext line_profiler

逐行計時的魔術命令

%lprun -f convert_units
撰寫高效的 Python 程式碼

程式碼分析:line_profiler

使用 line_profiler 套件

%load_ext line_profiler

逐行計時的魔術命令

%lprun -f convert_units convert_units(heroes, hts, wts)
撰寫高效的 Python 程式碼

%lprun 輸出

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="魔術命令 lprun 的表格輸出,摘要執行時間分析統計"

撰寫高效的 Python 程式碼

%lprun 輸出

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="魔術命令 lprun 的表格輸出,摘要執行時間分析統計,重點標示 Line # 與 Hits 欄"

撰寫高效的 Python 程式碼

%lprun 輸出

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="魔術命令 lprun 的表格輸出,摘要執行時間分析統計,重點標示 Time 欄"

撰寫高效的 Python 程式碼

%lprun 輸出

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="魔術命令 lprun 的表格輸出,摘要執行時間分析統計,重點標示第一列的 Time 單位"

撰寫高效的 Python 程式碼

%lprun 輸出

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="魔術命令 lprun 的表格輸出,摘要執行時間分析統計,重點標示第三列"

撰寫高效的 Python 程式碼

%lprun 輸出

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="魔術命令 lprun 的表格輸出,摘要執行時間分析統計,重點標示 Per Hit 欄"

撰寫高效的 Python 程式碼

%lprun 輸出

%lprun -f convert_units convert_units(heroes, hts, wts)

魔術命令 lprun 的表格輸出,摘要執行時間分析統計,重點標示第九列

撰寫高效的 Python 程式碼

%lprun 輸出

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="魔術命令 lprun 的表格輸出,摘要執行時間分析統計,重點標示 % Time 欄"

撰寫高效的 Python 程式碼

%lprun 輸出

%lprun -f convert_units convert_units(heroes, hts, wts)

alt="魔術命令 lprun 的表格輸出,摘要執行時間分析統計,重點標示 Line Contents 欄"

撰寫高效的 Python 程式碼
%timeit convert_units convert_units(heroes, hts, wts)
3 µs ± 32 ns 每回合(7 次執行的平均值 ± 標準差,每次 100000 回合)
%lprun -f convert_units convert_units(heroes, hts, wts)

alt="魔術命令 lprun 的表格輸出,摘要執行時間分析統計,重點標示第二列的 Total time"

撰寫高效的 Python 程式碼

一起來練習吧!

撰寫高效的 Python 程式碼

Preparing Video For Download...