跟著小編一起學習7個Python性能優化工具

雖然Python是一個”慢慢的“語言,但是不代表我們對性能沒有任何的追求,在程序運行過程中,如果發現程序運行時間太長或者內存佔用過大,免不了需要對程序的執行過程進行一些監測,找到有問題的地方,進行優化。今天來分享一些平時用到的Python性能分析工具

memory_profiler

memory_profiler是監控python進程的神器,只需要在函數加一個裝飾器就可以輸出每行代碼的內存使用情況

安裝:

<code>pip install memory_profiler/<code>

使用:

<code>import time@profiledef my_func(): a = [1] * (10 ** 6) b = [2] * (2 * 10 ** 7) time.sleep(10) del b del a print "+++++++++"if __name__ == '__main__': my_func()/<code>

輸出:

<code>$ python -m memory_profiler del3.py+++++++++Filename: del3.pyLine # Mem usage Increment Line Contents================================================ 10.293 MiB 0.000 MiB @profile def my_func(): 17.934 MiB 7.641 MiB a = [1] * (10 ** 6) 170.523 MiB 152.590 MiB b = [2] * (2 * 10 ** 7) 170.527 MiB 0.004 MiB time.sleep(10) 17.938 MiB -152.590 MiB del b 10.305 MiB -7.633 MiB del a 10.309 MiB 0.004 MiB print "+++++++++"/<code>

內建函數 timeit

<code>import timeitimport timedef my_func(): time.sleep(1) return sum([1,2,3])result = timeit.timeit(my_func, number=5)print(result)/<code>

Jupyter Notebook Magic 命令

在Jupyter Notebook中,可以通過 %%timeit 魔法命令測試cell中代碼的運行時間

<code>%%timeitimport timedef my_func(): time.sleep(1) return sum([1,2,3])result = timeit.timeit(my_func, number=5)print(result)/<code>

計時裝飾器

Python 中的裝飾器可以在其他函數不需要改動任何代碼的情況下增加額外功能,經常用在,插入日誌、性能測試、權限校驗等場景中。我們可以將計時功能封裝成一個裝飾器,方便複用。

<code>from functools import wrapsimport timedef timeit(func): @wraps(func) def deco(): start = time.time() res = func() end = time.time() delta = end - start print("Wall time ", delta) return res return deco/<code>

使用:

<code>@timeitdef my_func(): # do something time.sleep(3) pass/<code>

輸出:

<code>Wall time: 3/<code>

line_profiler

如果我們除了想知道代碼整體的運行時間之外,還要精確分析每行代碼的運行時間,那python的 line_profiler 模塊就可以幫到你啦!line_profiler 可以用來測試函數每行代碼的響應時間等情況。為了使用方便,可以將line_profiler 相關函數封裝在裝飾器中進行使用,這樣在接口請求時,則會執行此裝飾器並打印出結果。

安裝:

<code>pip install line_profiler/<code>

使用:

<code>from flask import Flask, jsonifyimport timefrom functools import wrapsfrom line_profiler import LineProfiler# 查詢接口中每行代碼執行的時間def func_line_time(f): @wraps(f) def decorator(*args, **kwargs): func_return = f(*args, **kwargs) lp = LineProfiler() lp_wrap = lp(f) lp_wrap(*args, **kwargs)  lp.print_stats()  return func_return  return decoratorapp = Flask(__name__)@app.route('/line_test') @func_line_time def line_test():  for item in range(5):  time.sleep(1)  for item in xrange(5):  time.sleep(0.5)  return jsonify({'code':200}) if __name__=='__main__': app.run()/<code>

輸出:

<code>* Running on http://127.0.0.1:5000/Timer unit: 1e-06 sTotal time: 7.50827 sFile: /home/rgc/baidu_eye/carrier/test/flask_line_profiler_test.pyFunction: line_test at line 22Line # Hits Time Per Hit % Time Line Contents============================================================== @app.route('/line_test') @func_line_time def line_test(): 6 33.0 5.5 0.0 for item in range(5): 5 5005225.0 1001045.0 66.7 time.sleep(1) 6 31.0 5.2 0.0 for item in xrange(5): 5 2502696.0 500539.2 33.3 time.sleep(0.5) 1 282.0 282.0 0.0 return jsonify({'code':200})127.0.0.1 - - [05/Mar/2018 15:58:21] "GET /line_test HTTP/1.1" 200 -/<code>

pyheat

相較於上面的代碼運行時間測試工具,pyheat 通過matplotlib 的繪製熱力圖來展現代碼的運行時間,顯得更為直觀

跟著小編一起學習7個Python性能優化工具

安裝:

<code>pip install py-heat/<code>

使用方法:

<code>pyheat <path> --out image_file.png/<path>/<code>

heartrate

heartrate 也是一個可視化的監測工具,可以像監測心率一樣追蹤程序運行,通過web頁面可視化Python程序的執行過程。


跟著小編一起學習7個Python性能優化工具


img

左側數字表示每行代碼被觸發的次數。長方框表示最近被觸發的代碼行——方框越長表示觸發次數越多,顏色越淺表示最近被觸發次數越多。該工具記錄的是每行代碼執行的次數,

而不是具體執行時間,在性能調試的時候有些雞肋

安裝:

<code>pip install --user heartrate/<code>

使用:

<code>import heartratefrom heartrate import trace, filesheartrate.trace(browser=True)trace(files=files.path_contains('my/<code>
跟著小編一起學習7個Python性能優化工具

最後小編幫助大家整理了一套python教程,下面展示了部分,希望也能幫助對編程感興趣,想做數據分析,人工智能、爬蟲或者希望從事編程開發的小夥伴,畢竟python工資也還可以,如果能幫到你請點贊、點贊、點贊哦~~


跟著小編一起學習7個Python性能優化工具


跟著小編一起學習7個Python性能優化工具


跟著小編一起學習7個Python性能優化工具


分享到:


相關文章: