採用下列指令安裝,會需要有 Visual Studio 才能夠順利安裝:
可到 Unofficial Windows Binaries for Python Extension Packages 搜尋 line_profiler,下載對應版本的 .whl。$ pip install line_profiler
然後以下列指令安裝 (for Python 3.7):
試著跑跑看:$ pip install line_profiler‑3.0.2‑cp37‑cp37m‑win_amd64.whl . . . Successfully installed line-profiler-3.0.2
$ ipython In[1]: def sum_of_lists(N): total = 0 for i in range(5): L = [j ^ (j >> i) for j in range(N)] total += sum(L) return total In[2]: %load_ext line_profiler In[3]: %lprun -f sum_of_lists sum_of_lists(5000) Timer unit: 1e-07 s Total time: 0.0130998 s File:
Function: sum_of_lists at line 1 Line # Hits Time Per Hit % Time Line Contents ============================================================== 1 def sum_of_lists(N): 2 1 21.0 21.0 0.0 total = 0 3 6 102.0 17.0 0.1 for i in range(5): 4 5 126352.0 25270.4 96.5 L = [j ^ (j >> i) for j in range(N)] 5 5 4515.0 903.0 3.4 total += sum(L) 6 1 8.0 8.0 0.0 return total
參考資料
[書籍] Python Data Science Handbook, Jake VanderPlas