Archive for the ‘Programming’ Category

[Python] Evolution of a Python programmer.py

เจอผ่านๆ ลูกตา รู้สึกประทับใจ เรยเก็บไว้หน่อย นะๆๆ

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)

[ComKUCamp] Introduction GPU Programming

Introduction to GPU Programming
View more documents from Chakkirt Tantithamthavorn.

Example Application of GPU
View more documents from Chakkirt Tantithamthavorn.

[Python] Optimize what needs optimizing?

ปกติแล้ว เวลาผมเขียนโปรแกรมจะถือคตินี้มาตลอด นั่นคือ
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 [...]

[Python] Identify the bottlenecks by Python profilers

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__’:
[...]

Comparing the well-known sorting algorithm

ไหนๆ เรียนวิชา 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] [...]

[Ubuntu] Mono C# installation with terminal

ตะกี๊ @pamon ไม่สามารถ run C# บน Ubuntu ไปดูวิธี Setup Environment กันหน่อยดีกว่า
คุ้นๆกันดีว่า มี โมโน(Mono) บนระบบ Unix ที่สามารถ Run .Net Framework ได้ Cross-Platform ทั้ง Unix, Windows และ Mac ซึ่งปัจจุบันออกไปถึง Version 2.4 ซะแล้ว(ณ วันที่ 18 June 2009)
วิธีการลง Mono บน Ubuntu ทำตามนี้โลด

sudo apt-get install mono-devel mono-mcs

Method Swap – without temporary variable

ตัวอย่างข้างต้น เป็นการเขียนโปรแกรม swap ค่าแบบแปลกๆครับ ไม่ต้องมาสร้างตัวแปร temp อีกแย๊ว
จะนำเสนอ 2 วิธีครับ
วิธีแรก :: การใช้ Operator XOR
ตัวอย่าง code สำหรับ ภาษา C#

1
2
3
4
5
void swap (ref int a, ref int b) {
a ^= b;
b ^= a;
a ^= b;
}

ตัวอย่าง code สำหรับ ภาษา C/C++

1
2
3
4
5
void swap (int &a, int &b) [...]

Basic way to solve the problems.

Make it work. Make it right. Then make it fast.
หมายความว่ายังไง
Make it work. เีขียนโปรแกรมให้มันทำงานได้ซะก่อน
Make it right. เขียนโปรแกรมให้มันแสดงผลได้ถูกต้อง
Then make it fast. เขียนโปรแกรมให้มันทำงานได้เร็วที่สุดและ optimize ที่สุด ซึ่งสอดคล้องกับ bigO อะไรอย่างงั้น
มันก็เป็นเพียงแค่หลักการเบื้องต้น เล็กๆ ที่ดูดี แค่นั้นเองครับ