Web2.0, HPC, Unix, and my mine.
Posts tagged performance
[Python] Optimize what needs optimizing?
Jan 5th
ปกติแล้ว เวลาผมเขียนโปรแกรมจะถือคตินี้มาตลอด นั่นคือ
Make it done, make it right, and then make it fast.
blog นี้จะมาบอกว่า เราจะ opimize โปรแกรมที่เขียนด้วยภาษา python ได้อย่างไรบ้าง
อย่างแรก ให้ติดตั้ง Python profiler ก่อน คลิก เพื่อดูว่าแต่ละ function ใช้เวลาในการทำงานนานเท่าไหร่
blog นี้ ผมจะสรุปจาก PerformanceTips ละกันครับ
- เลือก Data structure ให้เหมาะสม
- ใช้ Sorting ของ Python
อ้างอิงจาก Comparing the well-known sorting algorithm ซึ่งผมเคยเทียบประสิทธิภาพไปแล้วว่า python sorting เร็วกว่าใครเพื่อน นอกจากนั้น Guido van Rossum ยังแนะนำให้ใช้ comparator - String Concatenation
หลีกเลี่ยง (เอา string ใน list มา concat กัน)s = "" for substring in list: s += substring
แนะนำ
s = "".join(list)
หลีกเลี่ยง
out = "" + head + prologue + query + tail + ""
แนะนำ
out = "%s%s%s%s" % (head, prologue, query, tail)
แต่จะดูดีและเร็วขึ้นอีก ถ้าเขียนแบบนี้ (โฮก!! หล่อมาก++)
out = "%(head)s%(prologue)s%(query)s%(tail)s" % locals()
- Loops แนะนำให้ใช้ map-function
หลีกเลี่ยง (แปลง string ใน list เป็น upper case)newlist = [] for word in oldlist: newlist.append(word.upper())
แนะนำ
newlist = map(str.upper, oldlist)
หรือ
newlist = (s.upper() for s in oldlist)
- Avoiding dot…
หลีกเลี่ยงการใช้ method chaining เช่น ‘AbCDe1234′.lower().count(‘a’) เป็นต้น - Local Variable
Python สามารถ access local variable ได้เร็วกว่า global variable - Initialize dictionary element
แนะนำให้ตั้งค่าเริ่มต้นให้กับ dictionarywdict = {} for word in words: if word not in wdict: wdict[word] = 0 wdict[word] += 1
หรือเขียนได้อีกแบบ
wdict = {} for word in words: try: wdict[word] += 1 except KeyError: wdict[word] = 1
- import statement overhead
ทุกคำสั่ง import จะมี overhead เสมอ และหลีกเลี่ยงการใช้ import ภายใน function เช่นdef foo(): import os ...
- Data aggregation
หลีกเลี่ยงการใช้ for loop ไปเรียก function
หลีกเลี่ยงimport time x = 0 def doit1(i): global x x = x + i list = range(100000) t = time.time() for i in list: doit1(i) print "%.3f" % (time.time()-t)
แนะนำ
import time x = 0 def doit2(list): global x for i in list: x = x + i list = range(100000) t = time.time() doit2(list) print "%.3f" % (time.time()-t)
- use xrange instead of range
โอ้ 10 ข้อแหนะ ถ้าทำได้หมดแล้วยังไม่เร็วขึ้น แนะนำให้ใช้ Python Psyco ละครับครับ
[Python] Identify the bottlenecks by Python profilers
Jan 5th
Python Profiler package เป็น package ไว้สำหรับ วิเคราะห์ประสิทธิภาพของโปรแกรมที่เขียนด้วยภาษา python เพื่อหา bottleneck หรือปัญหาคอขวดของโปรแกรมของเราว่าอยู่ตรงไหน
Minimum-requirement: Python 2.6 or later [use profiler and pstats package]
ถ้าเครื่องไหนไม่สามารถใช้งานได้ ให้ลงก่อน ด้วยคำสั่งดังกล่าว
$ sudo aptitude install python-profiler
ปกติแล้ว Python profilers จะมี standard library ชื่อว่า profile กับ cProfile ผมแนะนำให้ใช้ cProfile ดีกว่าครับ เพราะค่อนข้าง low-level กว่าและมี overhead ต่ำกว่าด้วยครับ
ตัวอย่างการใช้งานทั่วๆไป
import cProfile import pstats def foo(): ... if __name__ == '__main__': cProfile.run('foo()')
วิธีใช้งานคือ เรียกคำสั่ง cProfile.run(‘ ชื่อฟังก์ชั่น ‘) แค่นั้นเองครับ ง่ายมาก
ตัวอย่างการใช้งานเมื่อมีการส่ง parameter ให้ function
import cProfile import pstats def foo(bar): ... if __name__ == '__main__': cProfile.run('foo()',[bar])
วิธีใช้งานคือ เรียกคำสั่ง cProfile.run(‘ ชื่อฟังก์ชั่น ‘, list ของ parameter )
ตัวอย่างของผลลัพท์
2706 function calls (2004 primitive calls) in 4.504 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
2 0.006 0.003 0.953 0.477 pobject.py:75(save_objects)
43/3 0.533 0.012 0.749 0.250 pobject.py:99(evaluate)
...2706 function calls (2004 primitive calls) in 4.504 CPU seconds
หมายถึง มีการเรียก function 2706 ครั้ง ภายในเวลา 4.504 วินาที สำหรับ primitive calls หมายถึง จำนวนการเรียกฟังก์ชั่นแบบไม่ recursive นั่นคือ ถ้ามีการ recursive primitive จะนับแค่ 1 ครั้งเท่านั้น
ncalls หมายถึง จำนวนครั้งที่เรียก function นั้นๆๆ
tottime หมายถึง ปริมาณเวลาทั้งหมด ที่ใช้ในการเรียก function ทั้งหมด
percall หมายถึง สัดส่วน ncall/tottime
cumtime (cumulative time) เวลาที่ใช้ใน function นั้นๆ
percall หมายถึง สัดส่วน primitive call/cumulative time
filename:lineno(function) ระบุ ชื่อไฟล์ บรรทัดที่? และชื่อ function นั้นๆ
สำหรับรายละเอียดเพิ่มเติมสามารถดูได้จาก http://docs.python.org/library/profile.html
