自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

效率神器之快速定位最慢的代碼

開發(fā) 開發(fā)工具
今天分享一個(gè)超級(jí)實(shí)用的 Python 性能分析工具 pyinstrument ,可以快速找到代碼運(yùn)行最慢的部分,幫助提高代碼的性能。支持 Python 3.7+ 且能夠分析異步代碼,僅需一條命令即可顯示具體代碼的耗時(shí)。

[[422493]]

天下武功,唯快不破。

編程也不例外,你的代碼跑的快,你能快速找出代碼慢的原因,你的碼功就高。

今天分享一個(gè)超級(jí)實(shí)用的 Python 性能分析工具 pyinstrument ,可以快速找到代碼運(yùn)行最慢的部分,幫助提高代碼的性能。支持 Python 3.7+ 且能夠分析異步代碼,僅需一條命令即可顯示具體代碼的耗時(shí)。經(jīng)常寫 Python 的小伙伴一定要用一下。

安裝

  1. pip install pyinstrument 

簡(jiǎn)單的使用

在程序的開始,啟動(dòng) pyinstrument 的 Profiler,結(jié)束時(shí)關(guān)閉 Profiler 并打印分析結(jié)果如下:

  1. from pyinstrument import Profiler 
  2.  
  3. profiler = Profiler() 
  4. profiler.start() 
  5.  
  6. # 這里是你要分析的代碼 
  7.  
  8. profiler.stop() 
  9.  
  10. profiler.print() 

比如這段代碼 123.py,我們可以清楚的看到是列表推導(dǎo)式比較慢:

  1. from pyinstrument import Profiler 
  2.  
  3. profiler = Profiler() 
  4. profiler.start() 
  5.  
  6. # 這里是你要分析的代碼 
  7. a = [i for i in range(100000)] 
  8. b = (i for i in range(100000)) 
  9.  
  10. profiler.stop() 
  11. profiler.print() 

上述分析需要修改源代碼,如果你使用命令行工具,就不需要修改源代碼,只需要執(zhí)行 pyinstrument xxxx.py 即可:

比如有這樣一段排序的程序 c_sort.py:

  1. import sys 
  2. import time 
  3.  
  4. import numpy as np 
  5.  
  6. arr = np.random.randint(0, 10, 10) 
  7.  
  8. def slow_key(el): 
  9.     time.sleep(0.01) 
  10.     return el  
  11.  
  12. arr = list(arr) 
  13.  
  14. for i in range(10): 
  15.     arr.sort(key=slow_key) 
  16.  
  17. print(arr) 

這段代碼里面故意放了一句 time.sleep(0.01) 來延遲性能,看看 pyinstrument 能否識(shí)別,命令行執(zhí)行 pyinstrument c_sort.py:

從結(jié)果來看,程序運(yùn)行了 1.313 秒,而 sleep 就運(yùn)行了 1.219 秒,很明顯是瓶頸,現(xiàn)在我們把它刪除,再看看結(jié)果:

刪除之后,性能最慢的就是 numpy 模塊的初始化代碼 __init__.py了,不過這些代碼不是自己寫的,而且并不是特別慢,就不需要去關(guān)心了。

分析 Flask 代碼

Web 應(yīng)用也可以使用這個(gè)來找出性能瓶頸,比如 flask,只需要在請(qǐng)求之前記錄時(shí)間,在請(qǐng)求之后統(tǒng)計(jì)時(shí)間,只需要在 flask 的請(qǐng)求攔截器里面這樣寫:

  1. from flask import Flask, g, make_response, request 
  2. app = Flask(__name__) 
  3.  
  4. @app.before_request 
  5. def before_request(): 
  6.     if "profile" in request.args: 
  7.         g.profiler = Profiler() 
  8.         g.profiler.start() 
  9.  
  10.  
  11. @app.after_request 
  12. def after_request(response): 
  13.     if not hasattr(g, "profiler"): 
  14.         return response 
  15.     g.profiler.stop() 
  16.     output_html = g.profiler.output_html() 
  17.     return make_response(output_html) 

假如有這樣一個(gè) API:

  1. @app.route("/dosomething"
  2. def do_something(): 
  3.     import requests 
  4.     requests.get("http://google.com"
  5.     return "Google says hello!" 

為了測(cè)試這個(gè) API 的瓶頸,我們可以在 url 上加一個(gè)參數(shù) profile 就可以:http://127.0.0.1:5000/dosomething?profile,哪一行代碼執(zhí)行比較慢,結(jié)果清晰可見:

分析 Django 代碼

分析 Django 代碼也非常簡(jiǎn)單,只需要在 Django 的配置文件的 MIDDLEWARE 中添加

  1. "pyinstrument.middleware.ProfilerMiddleware"

然后就可以在 url 上加一個(gè)參數(shù) profile 就可以:

如果你不希望所有人都能看到,只希望管理員可以看到,settings.py 可以添加這樣的代碼:

  1. def custom_show_pyinstrument(request): 
  2.     return request.user.is_superuser 
  3.  
  4. PYINSTRUMENT_SHOW_CALLBACK = "%s.custom_show_pyinstrument" % __name__ 

如果不想通過 url 后面加參數(shù)的方式查看性能分析,可以在 settings.py 文件中添加:

  1. PYINSTRUMENT_PROFILE_DIR = 'profiles' 

這樣,每次訪問一次 Django 接口,就會(huì)將分析結(jié)果以 html 文件形式保存在 項(xiàng)目目錄下的 profiles 文件夾中。

分析異步代碼

簡(jiǎn)單的異步代碼分析:

  1. import asyncio 
  2.  
  3. from pyinstrument import Profiler 
  4.  
  5.  
  6. async def main(): 
  7.     p = Profiler() 
  8.     with p: 
  9.         print("Hello ..."
  10.         await asyncio.sleep(1) 
  11.         print("... World!"
  12.     p.print() 
  13.  
  14.  
  15. asyncio.run(main()) 

復(fù)雜一些的異步代碼分析:

  1. import asyncio 
  2. import time 
  3.  
  4. import pyinstrument 
  5.  
  6.  
  7. def do_nothing(): 
  8.     pass 
  9.  
  10.  
  11. def busy_wait(duration): 
  12.     end_time = time.time() + duration 
  13.  
  14.     while time.time() < end_time: 
  15.         do_nothing() 
  16.  
  17.  
  18. async def say(what, when, profile=False): 
  19.     if profile: 
  20.         p = pyinstrument.Profiler() 
  21.         p.start() 
  22.  
  23.     busy_wait(0.1) 
  24.     sleep_start = time.time() 
  25.     await asyncio.sleep(when
  26.     print(f"slept for {time.time() - sleep_start:.3f} seconds"
  27.     busy_wait(0.1) 
  28.  
  29.     print(what) 
  30.     if profile: 
  31.         p.stop() 
  32.         p.print(show_all=True
  33.  
  34.  
  35. loop = asyncio.get_event_loop() 
  36.  
  37. loop.create_task(say("first hello", 2, profile=True)) 
  38. loop.create_task(say("second hello", 1, profile=True)) 
  39. loop.create_task(say("third hello", 3, profile=True)) 
  40.  
  41. loop.run_forever() 
  42. loop.close() 

工作原理

Pyinstrument 每 1ms 中斷一次程序,并在該點(diǎn)記錄整個(gè)堆棧。它使用 C 擴(kuò)展名和 PyEval_SetProfile 來做到這一點(diǎn),但只每 1 毫秒讀取一次讀數(shù)。你可能覺得報(bào)告的樣本數(shù)量有點(diǎn)少,但別擔(dān)心,它不會(huì)降低準(zhǔn)確性。默認(rèn)間隔 1ms 是記錄堆棧幀的下限,但如果在單個(gè)函數(shù)調(diào)用中花費(fèi)了很長(zhǎng)時(shí)間,則會(huì)在該調(diào)用結(jié)束時(shí)進(jìn)行記錄。如此有效地將這些樣本“打包”并在最后記錄。

Pyinstrument 是一個(gè)統(tǒng)計(jì)分析器,并不跟蹤,它不會(huì)跟蹤您的程序進(jìn)行的每個(gè)函數(shù)調(diào)用。相反,它每 1 毫秒記錄一次調(diào)用堆棧。與其他分析器相比,統(tǒng)計(jì)分析器的開銷比跟蹤分析器低得多。

比如說,我想弄清楚為什么 Django 中的 Web 請(qǐng)求很慢。如果我使用 cProfile,我可能會(huì)得到這個(gè):

  1. 151940 function calls (147672 primitive calls) in 1.696 seconds 
  2.  
  3.    Ordered by: cumulative time 
  4.  
  5.    ncalls  tottime  percall  cumtime  percall filename:lineno(function
  6.         1    0.000    0.000    1.696    1.696 profile:0(<code object <module> at 0x1053d6a30, file "./manage.py", line 2>) 
  7.         1    0.001    0.001    1.693    1.693 manage.py:2(<module>) 
  8.         1    0.000    0.000    1.586    1.586 __init__.py:394(execute_from_command_line) 
  9.         1    0.000    0.000    1.586    1.586 __init__.py:350(execute
  10.         1    0.000    0.000    1.142    1.142 __init__.py:254(fetch_command) 
  11.        43    0.013    0.000    1.124    0.026 __init__.py:1(<module>) 
  12.       388    0.008    0.000    1.062    0.003 re.py:226(_compile) 
  13.       158    0.005    0.000    1.048    0.007 sre_compile.py:496(compile) 
  14.         1    0.001    0.001    1.042    1.042 __init__.py:78(get_commands) 
  15.       153    0.001    0.000    1.036    0.007 re.py:188(compile) 
  16.   106/102    0.001    0.000    1.030    0.010 __init__.py:52(__getattr__) 
  17.         1    0.000    0.000    1.029    1.029 __init__.py:31(_setup) 
  18.         1    0.000    0.000    1.021    1.021 __init__.py:57(_configure_logging) 
  19.         2    0.002    0.001    1.011    0.505 log.py:1(<module>) 

看完是不是還是一臉懵逼,通常很難理解您自己的代碼如何與這些跟蹤相關(guān)聯(lián)。Pyinstrument 記錄整個(gè)堆棧,因此跟蹤昂貴的調(diào)用要容易得多。它還默認(rèn)隱藏庫(kù)框架,讓您專注于影響性能的應(yīng)用程序/模塊:

  1.   _     ._   __/__   _ _  _  _ _/_   Recorded: 14:53:35  Samples:  131 
  2.  /_//_/// /_\ / //_// / //_'/ //    Duration: 3.131     CPU time: 0.195 
  3. /   _/                    v3.0.0b3 
  4.  
  5. Program: examples/django_example/manage.py runserver --nothreading --noreload 
  6.  
  7. 3.131 <module>  manage.py:2 
  8. └─ 3.118 execute_from_command_line  django/core/management/__init__.py:378 
  9.       [473 frames hidden]  django, socketserver, selectors, wsgi... 
  10.          2.836 select  selectors.py:365 
  11.          0.126 _get_response  django/core/handlers/base.py:96 
  12.          └─ 0.126 hello_world  django_example/views.py:4 

最后的話

本文分享了 pyinstrument 的用法,有了這個(gè)性能分析神器,以后優(yōu)化代碼可以節(jié)省很多時(shí)間了,這樣的效率神器很值得分享,畢竟人生苦短,能多點(diǎn)時(shí)間干點(diǎn)有意思的不香么?

本文轉(zhuǎn)載自微信公眾號(hào)「Python七號(hào)」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系Python七號(hào)公眾號(hào)。

 

責(zé)任編輯:武曉燕 來源: Python七號(hào)
相關(guān)推薦

2022-09-20 10:41:32

接口優(yōu)化網(wǎng)絡(luò)

2024-09-06 08:02:52

2019-06-26 09:20:38

Java工具Lombok

2023-11-22 08:26:03

HutoolJava工具集

2020-11-25 19:57:15

開發(fā)技能代碼

2020-03-18 15:54:41

開發(fā)效率代碼

2022-09-26 00:00:01

java代碼開發(fā)

2025-02-27 09:45:47

2019-11-27 10:08:36

程序員技能開發(fā)者

2020-08-11 08:11:40

JavaScript開發(fā)技術(shù)

2021-02-20 08:05:35

代碼效率C++

2013-12-18 11:04:57

CPU雙核

2011-07-26 14:31:47

XCode 調(diào)試 全局

2023-05-10 09:24:10

TypeScript工具

2018-01-19 09:00:37

2021-04-25 07:47:36

電腦軟件EverythingIDM

2022-04-20 09:26:08

Mock前端開發(fā)工具

2012-07-16 01:20:09

代碼效率

2023-01-06 07:52:52

代碼生成器開發(fā)

2022-12-09 14:40:16

CPU進(jìn)程快速定位
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)