Web2.0, HPC, Unix, and my mine.
python
แค่ update python version จาก 2.6.4 ไป 3.1.2
Jul 11th
แม่มสงสัยอยู่ตั้งนานว่า ทำไม update python version 3.1.2 บนเครื่อง cloud2 แล้วมันแปลกๆ
แค่ลอง print ‘test’ มันก็ฟ้อง error ไอ้เราก็หาวิธีแก้อยู่ตั้งนาน
ที่ไหนได้ python 3.1.2 มันบอกว่า print is a function แล้วนะเธอว์ๆๆๆๆๆ แสดเอ๊ย ใครจะไปรู้ อุตส่าห์ download sourcecode มาแล้ว เลย ลงผ่าน source code เรยละกัน
ดูวิธีติดตั้งกันหน่อยละกัน ของเดิมๆ สำหรับคนทำเป็นแล้ว
1.) download sourcecode จาก http://python.org/ftp/python/3.1.2/Python-3.1.2.tgz
2.) พิมพ์คำสั่ง
./configure make make install
ปล. ความแตกต่างของ python 3.0+ คลิก
เหล่า CPE24 ทั้งหลายจงเสพย์ python 3.1.2 นะเธอวววววว์
[Python] Evolution of a Python programmer.py
Feb 1st
เจอผ่านๆ ลูกตา รู้สึกประทับใจ เรยเก็บไว้หน่อย นะๆๆ
1 2 3 4 5 6 7 | #Newbie programmer def factorial(x): if x == 0: return 1 else: return x * factorial(x - 1) print factorial(6) |
[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
Comparing the well-known sorting algorithm
Dec 26th
ไหนๆ เรียนวิชา Algorithm มาแล้ว มาเขียน Blog ให้เป็นประโยชน์กะรุ่นน้องหน่อยละกัน
Bubble Sort [O(n²)]
1 2 3 4 5 6 7 8 9 def bubble(list): swap = True while swap: swap = False for i in range(len(list)-1): if list[i] > list[i+1] : list[i],list[i+1] = list[i+1],list[i] swap = True return list

