|
| 1 | +--- |
| 2 | +author: kittinan |
| 3 | +layout: post |
| 4 | +title: "[Python] profiler ด้วย line_profiler" |
| 5 | +description: "internet" |
| 6 | +tags: [python, line_profiler] |
| 7 | +image: |
| 8 | + feature: /post/line_profiler/kernprof.png |
| 9 | +comments: true |
| 10 | +share: true |
| 11 | +date: 2019-02-08 22:30:00 |
| 12 | +--- |
| 13 | + |
| 14 | +## เกริ่นนำ |
| 15 | +ในการเขียนโปรแกรมนอกจากความถูกต้องในการทำงานของโปรแกรมแล้ว เราต้องการโปรแกรมที่ทำงานได้รวดเร็ว แต่บางทีเราก็ไม่รู้ว่าจะต้อง Optimize โปรแกรมกันตรงไหน วันนี้ผมจึงมาแนะนำการใช้งาน [line_profiler](https://github.com/rkern/line_profiler) เพื่อทำ profiler ในการตรวจสอบความเร็วในการทำงานของโค้ด Python ในแต่ละบรรทัดกันเลย |
| 16 | + |
| 17 | + |
| 18 | +## ติดตั้ง |
| 19 | +ติดตั้ง Package ผ่าน pip กันตามปกติ |
| 20 | + |
| 21 | +```bash |
| 22 | +pip install line_profiler |
| 23 | +``` |
| 24 | + |
| 25 | +### แบบ Annotation |
| 26 | + |
| 27 | +เพียงแค่เพิ่ม annotation @profile ไว้ก่อนฟังชั่นที่ต้องการตรวจสอบประสิทธิภาพเท่านั้น |
| 28 | + |
| 29 | +```python |
| 30 | +import time |
| 31 | + |
| 32 | +@profile |
| 33 | +def number(): |
| 34 | + s = 0 |
| 35 | + for i in range(500): |
| 36 | + s += i |
| 37 | + time.sleep(1) |
| 38 | + return s |
| 39 | + |
| 40 | +print(number()) |
| 41 | +``` |
| 42 | + |
| 43 | +จากโค้ดด้านบนผมได้เซฟไฟล์ไว้ชื่อ test.py และทำการรันด้วยคำสั่ง |
| 44 | + |
| 45 | +```bash |
| 46 | +kernprof -l test.py |
| 47 | +``` |
| 48 | + |
| 49 | +ก็จะมีไฟล์ test.py.lprof ปรากฎขึ้นมา เราก็แสดงผลด้วยคำสั่ง |
| 50 | + |
| 51 | +```bash |
| 52 | +python -m line_profiler test.py.lprof |
| 53 | +``` |
| 54 | + |
| 55 | +<figure><center> |
| 56 | + <img src="/images/post/line_profiler/kernprof.png" data-action="zoom"/> |
| 57 | + |
| 58 | + <figcaption> |
| 59 | + <a title="Inline Code 1"> |
| 60 | + ผลลัพธ์ที่ได้ |
| 61 | + </a> |
| 62 | + </figcaption> |
| 63 | +</center></figure> |
| 64 | + |
| 65 | +### วิธีการดูผลลัพท์ |
| 66 | + |
| 67 | +- Hit - จำนวนครั้งที่บรรทัดนี้ถูกทำงาน |
| 68 | +- Time - เวลาที่ใช้ในการทำงาน หน่วยเป็น Microsecond |
| 69 | +- Per Hit - เวลาเฉลี่ยเมื่อบรรทัดนี้ถูกทำงาน หน่วยเป็น Microsecond |
| 70 | +- % Time - สัดส่วนเวลาทั้งหมดที่ใช้คิดเป็น % |
| 71 | + |
| 72 | +จากตัวอย่างด้านบนก็จะเห็นว่าช้าที่สุดนั้นอยู่ตรงบรรทัดที่ sleep ไป 1 วินาที |
| 73 | + |
| 74 | +### Jupyter Notebook ก็ใช้ได้ |
| 75 | + |
| 76 | +หากใครใช้งาน Jupyter notebook ก็สามารถใช้ line_profiler ได้เหมือนกัน เพียงแค่โหลด extension line_profiler ขึ้นมาด้วยคำสั่ง |
| 77 | + |
| 78 | +```python |
| 79 | +%load_ext line_profiler |
| 80 | +``` |
| 81 | + |
| 82 | +หากต้องการตรวจสอบประสิทธิภาพของ function ก็แค่รัน function นั้นใน format นี้ |
| 83 | + |
| 84 | +```python |
| 85 | +%lprun -f [function name] [call function] |
| 86 | + |
| 87 | +#Example |
| 88 | +%lprun -f number number() |
| 89 | +``` |
| 90 | + |
| 91 | +<figure><center> |
| 92 | + <img src="/images/post/line_profiler/notebook_prof.png" data-action="zoom"/> |
| 93 | + |
| 94 | + <figcaption> |
| 95 | + <a title="Inline Code 1"> |
| 96 | + ตัวอย่างการใช้งาน line_profiler บน Google Colab |
| 97 | + </a> |
| 98 | + </figcaption> |
| 99 | +</center></figure> |
| 100 | + |
| 101 | +เมื่อเรารันคำสั่งก็จะมี popup เด้งขึ้นมาแสดงผลข้อมูลของแต่ละบรรทัด |
| 102 | + |
| 103 | +## สุดท้าย |
| 104 | +[line_profiler](https://github.com/rkern/line_profiler) สามารถทำ profiler โค้ดเราในแต่ละบรรทัดได้ง่ายมาก จึงหวังว่าจะเป็นประโยชน์แก่ทุกท่าน |
0 commit comments