Open1

爆速Pythonで手を動かしてみた

isseiissei

爆速Pythonの2.2.2 ラインプロファイリング、使ってみたら環境が新しくなっていたようなのでメモ。

アノテーション

こう書いた

@line_profiler.profile

実行時

シェル変数を渡した(modern環境側を参照 https://github.com/pyutils/line_profiler?tab=readme-ov-file#quick-start-modern

LINE_PROFILE=1 python distance_cache.py
python -m line_profiler profile_output.lprof

実行時間は数分だった。

Timer unit: 1e-06 s

Total time: 188.452 s
File: /Users/issei/programing/fast-python/chap02/distance_cache.py
Function: get_distances at line 34

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    34                                           @line_profiler.profile
    35                                           def get_distances(stations, locations):
    36         1          2.0      2.0      0.0    distances = {}
    37     13545       1880.0      0.1      0.0    for first_i in range(len(stations) - 1):
    38     13544       1367.0      0.1      0.0      first_station = stations[first_i]
    39     13544       1284.0      0.1      0.0      first_location = locations[first_station]
    40  91753828    6889370.0      0.1      3.7      for second_i in range(first_i, len(stations)):
    41  91740284    6573094.0      0.1      3.5        second_station = stations[second_i]
    42  91740284    8185821.0      0.1      4.3        second_location = locations[second_station]
    43 183480568  161759680.0      0.9     85.8        distances[(first_station, second_station)] = get_distance(
    44  91740284    5039174.0      0.1      2.7            first_location, second_location)
    45         1          0.0      0.0      0.0    return distances