line_profiler是一款監測python的CPU密集型性能問題的強大工具,可以對函數進行逐行分析,在linux上安裝時一切正常,然而今天在win10 64位系統安裝失敗了
pip3 install line_profiler
報錯:
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
提示我需要安裝 Microsoft Visual C++ 14.0
解決辦法:
wheel是一個已經編譯好的包,在安裝時不需要編譯過程,安裝whl文件時要比發布的源文件安裝要快,目前大部分python包都有wheel包,推薦使用wheel方式安裝
從https://www.lfd.uci.edu/~gohlke/pythonlibs/網站找到line_profiler,找到對應的python版本,實測python3.6.4下win_32版本安裝不成功,使用win_amd64能成功安裝
簡單測試:
kernprof -l -v tr1y.py
正常輸出如下:
(1.800000000000033, -1.800000000000033) length of x: 1000 total elements: 1000000 Wrote profile results to tr1y.py.lprof Timer unit: 5.68888e-07 s Total time: 2.84954 s File: tr1y.py Function: calculate_z_serial_purepython at line 48 Line # Hits Time Per Hit % Time Line Contents ============================================================== 48 @profile 49 def calculate_z_serial_purepython(max_iter, zs, cs): 50 1 7062.0 7062.0 0.1 output = [0] * len(zs) 51 1000001 564275.0 0.6 11.3 for i in range(len(zs)): 52 1000000 553309.0 0.6 11.0 n = 0 53 1000000 632514.0 0.6 12.6 z = zs[i] 54 1000000 593978.0 0.6 11.9 c = cs[i] 55 1485000 1365751.0 0.9 27.3 while abs(z) < 2 and n < max_iter: 56 485000 344250.0 0.7 6.9 z = z * z + c 57 485000 310470.0 0.6 6.2 n += 1 58 1000000 637342.0 0.6 12.7 output[i] = n 59 1 3.0 3.0 0.0 return output