日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python输出程序运行时间_叨叨 Python 性能优化工具

發布時間:2023/12/20 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python输出程序运行时间_叨叨 Python 性能优化工具 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

memory_profiler

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

安裝:

pip install memory_profiler

使用:

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()

輸出:

$ 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 "+++++++++"

內建函數 timeit

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

Jupyter Notebook Magic 命令

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

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

計時裝飾器

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

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

使用:

@timeitdef my_func(): # do something time.sleep(3) pass

輸出:

Wall time: 3

line_profiler

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

安裝:

pip install line_profiler

使用:

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()

輸出:

* 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 -

pyheat

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

安裝:

pip install py-heat

使用方法:

pyheat --out image_file.png

heartrate

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

img

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

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

安裝:

pip install --user heartrate

使用:

import heartratefrom heartrate import trace, filesheartrate.trace(browser=True)trace(files=files.path_contains('my_app', 'my_library'))

總結

以上是生活随笔為你收集整理的python输出程序运行时间_叨叨 Python 性能优化工具的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。