<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chakkrit Tantithamthavorn&#039;s Official Personal Website &#187; Programming</title>
	<atom:link href="http://www.klainfo.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.klainfo.com</link>
	<description></description>
	<lastBuildDate>Wed, 01 Feb 2012 13:12:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>แค่ update python version จาก 2.6.4 ไป 3.1.2</title>
		<link>http://www.klainfo.com/2010/07/11/%e0%b9%81%e0%b8%84%e0%b9%88-update-python-version-%e0%b8%88%e0%b8%b2%e0%b8%81-2-6-4-%e0%b9%84%e0%b8%9b-3-1-2/</link>
		<comments>http://www.klainfo.com/2010/07/11/%e0%b9%81%e0%b8%84%e0%b9%88-update-python-version-%e0%b8%88%e0%b8%b2%e0%b8%81-2-6-4-%e0%b9%84%e0%b8%9b-3-1-2/#comments</comments>
		<pubDate>Sat, 10 Jul 2010 17:48:10 +0000</pubDate>
		<dc:creator>klainfo</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[klainfo]]></category>
		<category><![CDATA[Mikelab]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[print is a function]]></category>
		<category><![CDATA[python 2.6.4]]></category>
		<category><![CDATA[python 3.1.2]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[updated]]></category>
		<category><![CDATA[version]]></category>

		<guid isPermaLink="false">http://www.klainfo.com/?p=810</guid>
		<description><![CDATA[แม่มสงสัยอยู่ตั้งนานว่า ทำไม update python version 3.1.2 บนเครื่อง cloud2 แล้วมันแปลกๆ แค่ลอง print &#8216;test&#8217; มันก็ฟ้อง 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+  คลิก [...]]]></description>
			<content:encoded><![CDATA[<p>แม่มสงสัยอยู่ตั้งนานว่า ทำไม update python version 3.1.2 บนเครื่อง cloud2 แล้วมันแปลกๆ<br />
แค่ลอง print &#8216;test&#8217; มันก็ฟ้อง error   ไอ้เราก็หาวิธีแก้อยู่ตั้งนาน</p>
<p>ที่ไหนได้ python 3.1.2 มันบอกว่า <strong>print is a function</strong> แล้วนะเธอว์ๆๆๆๆๆ แสดเอ๊ย ใครจะไปรู้ อุตส่าห์ download sourcecode มาแล้ว  เลย ลงผ่าน source code เรยละกัน</p>
<p>ดูวิธีติดตั้งกันหน่อยละกัน ของเดิมๆ สำหรับคนทำเป็นแล้ว</p>
<p>1.) download sourcecode จาก <a href="http://python.org/ftp/python/3.1.2/Python-3.1.2.tgz" target="_blank">http://python.org/ftp/python/3.1.2/Python-3.1.2.tgz</a></p>
<p>2.) พิมพ์คำสั่ง</p>
<pre>./configure
make
make install</pre>
<p>ปล. ความแตกต่างของ python 3.0+  <a href="http://docs.python.org/py3k/whatsnew/3.0.html" target="_blank">คลิก</a></p>
<h3>เหล่า CPE24 ทั้งหลายจงเสพย์ python 3.1.2 นะเธอวววววว์</h3>
]]></content:encoded>
			<wfw:commentRss>http://www.klainfo.com/2010/07/11/%e0%b9%81%e0%b8%84%e0%b9%88-update-python-version-%e0%b8%88%e0%b8%b2%e0%b8%81-2-6-4-%e0%b9%84%e0%b8%9b-3-1-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Python] Evolution of a Python programmer.py</title>
		<link>http://www.klainfo.com/2010/02/01/python-evolution-of-a-python-programmer-py/</link>
		<comments>http://www.klainfo.com/2010/02/01/python-evolution-of-a-python-programmer-py/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 18:20:37 +0000</pubDate>
		<dc:creator>klainfo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[evolution]]></category>
		<category><![CDATA[factorial]]></category>

		<guid isPermaLink="false">http://blog.klainfo.com/?p=612</guid>
		<description><![CDATA[เจอผ่านๆ ลูกตา รู้สึกประทับใจ เรยเก็บไว้หน่อย นะๆๆ 1 2 3 4 5 6 7 #Newbie programmer def factorial&#40;x&#41;: if x == 0: return 1 else: return x * factorial&#40;x - 1&#41; print factorial&#40;6&#41; 1 2 3 4 5 6 7 8 9 #First year programmer, studied Pascal def factorial&#40;x&#41;: result = 1 i = 2 while i [...]]]></description>
			<content:encoded><![CDATA[<p>เจอผ่านๆ ลูกตา รู้สึกประทับใจ เรยเก็บไว้หน่อย นะๆๆ</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#Newbie programmer</span>
<span style="color: #ff7700;font-weight:bold;">def</span> factorial<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> x == <span style="color: #ff4500;">0</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> x <span style="color: #66cc66;">*</span> factorial<span style="color: black;">&#40;</span>x - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> factorial<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<p><span id="more-612"></span></p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#First year programmer, studied Pascal</span>
<span style="color: #ff7700;font-weight:bold;">def</span> factorial<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
    result = <span style="color: #ff4500;">1</span>
    i = <span style="color: #ff4500;">2</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> i <span style="color: #66cc66;">&lt;</span>= x:
        result = result <span style="color: #66cc66;">*</span> i
        i = i + <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> result
<span style="color: #ff7700;font-weight:bold;">print</span> factorial<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#First year programmer, studied C</span>
<span style="color: #ff7700;font-weight:bold;">def</span> fact<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;">#{</span>
    result = i = <span style="color: #ff4500;">1</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span>i <span style="color: #66cc66;">&lt;</span>= x<span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;">#{</span>
        result <span style="color: #66cc66;">*</span>= i<span style="color: #66cc66;">;</span>
        i += <span style="color: #ff4500;">1</span><span style="color: #66cc66;">;</span>
    <span style="color: #808080; font-style: italic;">#}</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> result<span style="color: #66cc66;">;</span>
<span style="color: #808080; font-style: italic;">#}</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>fact<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#First year programmer, SICP</span>
@tailcall
<span style="color: #ff7700;font-weight:bold;">def</span> fact<span style="color: black;">&#40;</span>x, acc=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>x <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span>fact<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>x - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span>acc <span style="color: #66cc66;">*</span> x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:       <span style="color: #ff7700;font-weight:bold;">return</span> acc
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span>fact<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#First year programmer, Python</span>
<span style="color: #ff7700;font-weight:bold;">def</span> Factorial<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
    res = <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, x + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        res <span style="color: #66cc66;">*</span>= i
    <span style="color: #ff7700;font-weight:bold;">return</span> res
<span style="color: #ff7700;font-weight:bold;">print</span> Factorial<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">#Lazy Python programmer</span>
<span style="color: #ff7700;font-weight:bold;">def</span> fact<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> x <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">and</span> x <span style="color: #66cc66;">*</span> fact<span style="color: black;">&#40;</span>x - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">print</span> fact<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">#Lazier Python programmer</span>
f = <span style="color: #ff7700;font-weight:bold;">lambda</span> x: x <span style="color: #ff7700;font-weight:bold;">and</span> x <span style="color: #66cc66;">*</span> f<span style="color: black;">&#40;</span>x - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">print</span> f<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#Python expert programmer</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">operator</span> <span style="color: #ff7700;font-weight:bold;">as</span> op
<span style="color: #ff7700;font-weight:bold;">import</span> functional <span style="color: #ff7700;font-weight:bold;">as</span> f
fact = <span style="color: #ff7700;font-weight:bold;">lambda</span> x: f.<span style="color: black;">foldl</span><span style="color: black;">&#40;</span>op.<span style="color: black;">mul</span>, <span style="color: #ff4500;">1</span>, <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, x + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> fact<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#Python hacker</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
@tailcall
<span style="color: #ff7700;font-weight:bold;">def</span> fact<span style="color: black;">&#40;</span>x, acc=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> x: <span style="color: #ff7700;font-weight:bold;">return</span> fact<span style="color: black;">&#40;</span>x.<span style="color: #0000cd;">__sub__</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>, acc.<span style="color: #0000cd;">__mul__</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> acc
<span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>fact<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#EXPERT PROGRAMMER</span>
<span style="color: #ff7700;font-weight:bold;">import</span> c_math
fact = c_math.<span style="color: black;">fact</span>
<span style="color: #ff7700;font-weight:bold;">print</span> fact<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#ENGLISH EXPERT PROGRAMMER</span>
<span style="color: #ff7700;font-weight:bold;">import</span> c_maths
fact = c_maths.<span style="color: black;">fact</span>
<span style="color: #ff7700;font-weight:bold;">print</span> fact<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#Web designer</span>
<span style="color: #ff7700;font-weight:bold;">def</span> factorial<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#-------------------------------------------------</span>
    <span style="color: #808080; font-style: italic;">#--- Code snippet from The Math Vault          ---</span>
    <span style="color: #808080; font-style: italic;">#--- Calculate factorial (C) Arthur Smith 1999 ---</span>
    <span style="color: #808080; font-style: italic;">#-------------------------------------------------</span>
    result = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
    i = <span style="color: #ff4500;">1</span> <span style="color: #808080; font-style: italic;">#Thanks Adam</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> i <span style="color: #66cc66;">&lt;</span>= x:
        <span style="color: #808080; font-style: italic;">#result = result * i  #It's faster to use *=</span>
        <span style="color: #808080; font-style: italic;">#result = str(result * result + i)</span>
           <span style="color: #808080; font-style: italic;">#result = int(result *= i) #??????</span>
        result <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> i<span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;">#result = int(str(result) * i)</span>
        i = i + <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> result
<span style="color: #ff7700;font-weight:bold;">print</span> factorial<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#Unix programmer</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">def</span> fact<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
    <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'factorial '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
fact<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#Windows programmer</span>
NULL = <span style="color: #008000;">None</span>
<span style="color: #ff7700;font-weight:bold;">def</span> CalculateAndPrintFactorialEx<span style="color: black;">&#40;</span>dwNumber,
                                 hOutputDevice,
                                 lpLparam,
                                 lpWparam,
                                 lpsscSecurity,
                                 <span style="color: #66cc66;">*</span>dwReserved<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> lpsscSecurity <span style="color: #66cc66;">!</span>= NULL:
        <span style="color: #ff7700;font-weight:bold;">return</span> NULL <span style="color: #808080; font-style: italic;">#Not implemented</span>
    dwResult = dwCounter = <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> dwCounter <span style="color: #66cc66;">&lt;</span>= dwNumber:
        dwResult <span style="color: #66cc66;">*</span>= dwCounter
        dwCounter += <span style="color: #ff4500;">1</span>
    hOutputDevice.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>dwResult<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    hOutputDevice.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
CalculateAndPrintFactorialEx<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span>, <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL<span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre>
</td>
<td class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#Enterprise programmer</span>
<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span>cls, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> cls<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Number<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> IntegralNumber<span style="color: black;">&#40;</span><span style="color: #008000;">int</span>, Number<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> toInt<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">new</span> <span style="color: black;">&#40;</span><span style="color: #008000;">int</span>, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> InternalBase<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, base<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">base</span> = base.<span style="color: black;">toInt</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> getBase<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">new</span> <span style="color: black;">&#40;</span>IntegralNumber, <span style="color: #008000;">self</span>.<span style="color: black;">base</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MathematicsSystem<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, ibase<span style="color: black;">&#41;</span>:
        Abstract
&nbsp;
    @<span style="color: #008000;">classmethod</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> getInstance<span style="color: black;">&#40;</span>cls, ibase<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            cls.__instance
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">AttributeError</span>:
            cls.__instance = <span style="color: #dc143c;">new</span> <span style="color: black;">&#40;</span>cls, ibase<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> cls.__instance
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> StandardMathematicsSystem<span style="color: black;">&#40;</span>MathematicsSystem<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, ibase<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> ibase.<span style="color: black;">getBase</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #dc143c;">new</span> <span style="color: black;">&#40;</span>IntegralNumber, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">NotImplementedError</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">base</span> = ibase.<span style="color: black;">getBase</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> calculateFactorial<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, target<span style="color: black;">&#41;</span>:
        result = <span style="color: #dc143c;">new</span> <span style="color: black;">&#40;</span>IntegralNumber, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        i = <span style="color: #dc143c;">new</span> <span style="color: black;">&#40;</span>IntegralNumber, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> i <span style="color: #66cc66;">&lt;</span>= target:
            result = result <span style="color: #66cc66;">*</span> i
            i = i + <span style="color: #dc143c;">new</span> <span style="color: black;">&#40;</span>IntegralNumber, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> result
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> StandardMathematicsSystem.<span style="color: black;">getInstance</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">new</span> <span style="color: black;">&#40;</span>InternalBase, <span style="color: #dc143c;">new</span> <span style="color: black;">&#40;</span>IntegralNumber, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">calculateFactorial</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">new</span> <span style="color: black;">&#40;</span>IntegralNumber, <span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<p>Greate Idea from <a href="http://gist.github.com/289467">http://gist.github.com/289467</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.klainfo.com/2010/02/01/python-evolution-of-a-python-programmer-py/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[ComKUCamp] Introduction GPU Programming</title>
		<link>http://www.klainfo.com/2010/01/17/comkucamp-introduction-gpu-programming/</link>
		<comments>http://www.klainfo.com/2010/01/17/comkucamp-introduction-gpu-programming/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 07:10:12 +0000</pubDate>
		<dc:creator>klainfo</dc:creator>
				<category><![CDATA[CPE]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[comkucamp]]></category>
		<category><![CDATA[cuda]]></category>
		<category><![CDATA[gpu]]></category>
		<category><![CDATA[klainfo]]></category>

		<guid isPermaLink="false">http://blog.klainfo.com/?p=570</guid>
		<description><![CDATA[Introduction to GPU Programming View more documents from Chakkirt Tantithamthavorn. Example Application of GPU View more documents from Chakkirt Tantithamthavorn.]]></description>
			<content:encoded><![CDATA[<div id="__ss_2932123" style="width: 425px; text-align: left;"><a style="font: 14px Helvetica,Arial,Sans-serif; display: block; margin: 12px 0 3px 0; text-decoration: underline;" title="Introduction to GPU Programming" href="http://www.slideshare.net/klainfo/introduction-to-gpu-programming">Introduction to GPU Programming</a><object style="margin: 0px;" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="355" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=introduction-100117010105-phpapp01&amp;rel=0&amp;stripped_title=introduction-to-gpu-programming" /><param name="allowfullscreen" value="true" /><embed style="margin: 0px;" type="application/x-shockwave-flash" width="425" height="355" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=introduction-100117010105-phpapp01&amp;rel=0&amp;stripped_title=introduction-to-gpu-programming" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<div style="font-size: 11px; font-family: tahoma,arial; height: 26px; padding-top: 2px;">View more <a style="text-decoration: underline;" href="http://www.slideshare.net/">documents</a> from <a style="text-decoration: underline;" href="http://www.slideshare.net/klainfo">Chakkirt Tantithamthavorn</a>.</div>
</div>
<div id="__ss_2932122" style="width: 425px; text-align: left;"><a style="font: 14px Helvetica,Arial,Sans-serif; display: block; margin: 12px 0 3px 0; text-decoration: underline;" title="Example Application of GPU" href="http://www.slideshare.net/klainfo/example-application-of-gpu">Example Application of GPU</a><object style="margin: 0px;" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="355" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=application-100117010109-phpapp01&amp;rel=0&amp;stripped_title=example-application-of-gpu" /><param name="allowfullscreen" value="true" /><embed style="margin: 0px;" type="application/x-shockwave-flash" width="425" height="355" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=application-100117010109-phpapp01&amp;rel=0&amp;stripped_title=example-application-of-gpu" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<div style="font-size: 11px; font-family: tahoma,arial; height: 26px; padding-top: 2px;">View more <a style="text-decoration: underline;" href="http://www.slideshare.net/">documents</a> from <a style="text-decoration: underline;" href="http://www.slideshare.net/klainfo">Chakkirt Tantithamthavorn</a>.</div>
<div style="font-size: 11px; font-family: tahoma,arial; height: 26px; padding-top: 2px;"></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.klainfo.com/2010/01/17/comkucamp-introduction-gpu-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Python] Optimize what needs optimizing?</title>
		<link>http://www.klainfo.com/2010/01/05/python-optimize-what-needs-optimizing/</link>
		<comments>http://www.klainfo.com/2010/01/05/python-optimize-what-needs-optimizing/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 01:50:51 +0000</pubDate>
		<dc:creator>klainfo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[optimizing]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[speed]]></category>

		<guid isPermaLink="false">http://blog.klainfo.com/?p=549</guid>
		<description><![CDATA[ปกติแล้ว เวลาผมเขียนโปรแกรมจะถือคตินี้มาตลอด นั่นคือ 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 [...]]]></description>
			<content:encoded><![CDATA[<p>ปกติแล้ว เวลาผมเขียนโปรแกรมจะถือคตินี้มาตลอด นั่นคือ</p>
<blockquote><p><strong>Make it done, make it right, and then make it fast.</strong></p></blockquote>
<p>blog นี้จะมาบอกว่า เราจะ opimize โปรแกรมที่เขียนด้วยภาษา python ได้อย่างไรบ้าง</p>
<p>อย่างแรก ให้ติดตั้ง Python profiler ก่อน <a href="http://klainfo.com/2010/01/05/identify-the-bottlenecks-by-python-profilers/">คลิก</a> เพื่อดูว่าแต่ละ function ใช้เวลาในการทำงานนานเท่าไหร่</p>
<p>blog นี้ ผมจะสรุปจาก <a href="http://wiki.python.org/moin/PythonSpeed/PerformanceTips">PerformanceTips</a> ละกันครับ</p>
<ol>
<li><strong>เลือก Data structure ให้เหมาะสม</strong></li>
<li><strong>ใช้ Sorting ของ Python</strong><br />
อ้างอิงจาก <a title="Comparing the well-known sorting algorithm" href="http://klainfo.com/2009/12/26/comparing-the-well-known-sorting-algorithm/">Comparing the well-known sorting algorithm</a> ซึ่งผมเคยเทียบประสิทธิภาพไปแล้วว่า python sorting เร็วกว่าใครเพื่อน นอกจากนั้น Guido van Rossum ยังแนะนำให้ใช้ comparator</li>
<li><strong>String Concatenation</strong><br />
<span style="color: #ff0000;"> หลีกเลี่ยง</span> (เอา string ใน list มา concat กัน)</p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;">s = <span style="color: #483d8b;">&quot;&quot;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> substring <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">list</span>:
    s += substring</pre>
</div>
</div>
<p><span style="color: #008000;">แนะนำ</span></p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;">s = <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">list</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
<p><span style="color: #ff0000;">หลีกเลี่ยง</span></p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;">out = <span style="color: #483d8b;">&quot;&quot;</span> + head + prologue + query + tail + <span style="color: #483d8b;">&quot;&quot;</span></pre>
</div>
</div>
<p><span style="color: #008000;">แนะนำ</span></p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;">out = <span style="color: #483d8b;">&quot;%s%s%s%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>head, prologue, query, tail<span style="color: black;">&#41;</span></pre>
</div>
</div>
<p><span style="color: #008000;"><strong>แต่จะดูดีและเร็วขึ้นอีก ถ้าเขียนแบบนี้ (โฮก!! หล่อมาก++)</strong></span></p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;">out = <span style="color: #483d8b;">&quot;%(head)s%(prologue)s%(query)s%(tail)s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">locals</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
</li>
<li><strong>Loops แนะนำให้ใช้ map-function</strong><br />
<span style="color: #ff0000;"> หลีกเลี่ยง</span> (แปลง string ใน list เป็น upper case)</p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;">newlist = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> word <span style="color: #ff7700;font-weight:bold;">in</span> oldlist:
    newlist.<span style="color: black;">append</span><span style="color: black;">&#40;</span>word.<span style="color: black;">upper</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
<p><span style="color: #008000;">แนะนำ</span></p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;">newlist = <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span>.<span style="color: black;">upper</span>, oldlist<span style="color: black;">&#41;</span></pre>
</div>
</div>
<p><span style="color: #008000;">หรือ</span></p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;">newlist = <span style="color: black;">&#40;</span>s.<span style="color: black;">upper</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> s <span style="color: #ff7700;font-weight:bold;">in</span> oldlist<span style="color: black;">&#41;</span></pre>
</div>
</div>
</li>
<li><strong>Avoiding dot&#8230;<br />
<span style="font-weight: normal;">หลีกเลี่ยงการใช้ method chaining เช่น &#8216;AbCDe1234&#8242;.lower().count(&#8216;a&#8217;) เป็นต้น </span></strong></li>
<li><strong>Local Variable<br />
<span style="font-weight: normal;">Python สามารถ access local variable ได้เร็วกว่า global variable</span> </strong></li>
<li><strong>Initialize dictionary element</strong><br />
แนะนำให้ตั้งค่าเริ่มต้นให้กับ dictionary</p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;">wdict = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> word <span style="color: #ff7700;font-weight:bold;">in</span> words:
    <span style="color: #ff7700;font-weight:bold;">if</span> word <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> wdict:
        wdict<span style="color: black;">&#91;</span>word<span style="color: black;">&#93;</span> = <span style="color: #ff4500;">0</span>
    wdict<span style="color: black;">&#91;</span>word<span style="color: black;">&#93;</span> += <span style="color: #ff4500;">1</span></pre>
</div>
</div>
<p>หรือเขียนได้อีกแบบ</p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;">wdict = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> word <span style="color: #ff7700;font-weight:bold;">in</span> words:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        wdict<span style="color: black;">&#91;</span>word<span style="color: black;">&#93;</span> += <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyError</span>:
        wdict<span style="color: black;">&#91;</span>word<span style="color: black;">&#93;</span> = <span style="color: #ff4500;">1</span></pre>
</div>
</div>
</li>
<li><strong>import statement overhead</strong><br />
ทุกคำสั่ง import จะมี overhead เสมอ และหลีกเลี่ยงการใช้ import ภายใน function เช่น</p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
    ...</pre>
</div>
</div>
</li>
<li><strong>Data aggregation</strong><br />
หลีกเลี่ยงการใช้ for loop ไปเรียก function<br />
หลีกเลี่ยง</p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
x = <span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">def</span> doit1<span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">global</span> x
    x = x + i
&nbsp;
<span style="color: #008000;">list</span> = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">100000</span><span style="color: black;">&#41;</span>
t = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">list</span>:
    doit1<span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>-t<span style="color: black;">&#41;</span></pre>
</div>
</div>
<p>แนะนำ</p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
x = <span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">def</span> doit2<span style="color: black;">&#40;</span><span style="color: #008000;">list</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">global</span> x
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">list</span>:
        x = x + i
&nbsp;
<span style="color: #008000;">list</span> = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">100000</span><span style="color: black;">&#41;</span>
t = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
doit2<span style="color: black;">&#40;</span><span style="color: #008000;">list</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%.3f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>-t<span style="color: black;">&#41;</span></pre>
</div>
</div>
</li>
<li><strong>use xrange instead of range </strong></li>
</ol>
<p>โอ้ 10 ข้อแหนะ ถ้าทำได้หมดแล้วยังไม่เร็วขึ้น แนะนำให้ใช้ <a href="http://nattster.siamdev.net/2009/03/psyco-python/">Python Psyco</a> ละครับครับ</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klainfo.com/2010/01/05/python-optimize-what-needs-optimizing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[Python] Identify the bottlenecks by Python profilers</title>
		<link>http://www.klainfo.com/2010/01/05/identify-the-bottlenecks-by-python-profilers/</link>
		<comments>http://www.klainfo.com/2010/01/05/identify-the-bottlenecks-by-python-profilers/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 00:56:33 +0000</pubDate>
		<dc:creator>klainfo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[optimizing]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[speed]]></category>

		<guid isPermaLink="false">http://blog.klainfo.com/?p=553</guid>
		<description><![CDATA[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 &#160; def foo&#40;&#41;: [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Python Profiler package</strong> เป็น package ไว้สำหรับ วิเคราะห์ประสิทธิภาพของโปรแกรมที่เขียนด้วยภาษา python เพื่อหา bottleneck หรือปัญหาคอขวดของโปรแกรมของเราว่าอยู่ตรงไหน</p>
<p><strong>Minimum-requirement:</strong> Python 2.6 or later [use profiler and pstats package]</p>
<p>ถ้าเครื่องไหนไม่สามารถใช้งานได้ ให้ลงก่อน ด้วยคำสั่งดังกล่าว</p>
<div class="wp_syntax">
<div class="code">
<pre class="shell" style="font-family:monospace;">$ sudo aptitude install python-profiler</pre>
</div>
</div>
<p>ปกติแล้ว Python profilers จะมี standard library ชื่อว่า profile กับ cProfile ผม<strong>แนะนำให้ใช้ cProfile </strong>ดีกว่าครับ เพราะค่อนข้าง low-level กว่าและมี overhead ต่ำกว่าด้วยครับ</p>
<p><strong>ตัวอย่างการใช้งานทั่วๆไป</strong></p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> cProfile
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">pstats</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    ...
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    cProfile.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'foo()'</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
<p>วิธีใช้งานคือ เรียกคำสั่ง  cProfile.run(&#8216; ชื่อฟังก์ชั่น &#8216;) แค่นั้นเองครับ ง่ายมาก</p>
<p><strong>ตัวอย่างการใช้งานเมื่อมีการส่ง parameter ให้ function</strong></p>
<div class="wp_syntax">
<div class="code">
<pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> cProfile
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">pstats</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span>bar<span style="color: black;">&#41;</span>:
    ...
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    cProfile.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'foo()'</span>,<span style="color: black;">&#91;</span>bar<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
<p>วิธีใช้งานคือ เรียกคำสั่ง  cProfile.run(&#8216; ชื่อฟังก์ชั่น &#8216;, list ของ parameter )</p>
<p>ตัวอย่างของผลลัพท์</p>
<div class="wp_syntax">
<div class="code">
<pre class="text" style="font-family:monospace;">      2706 function calls (2004 primitive calls) in 4.504 CPU seconds
&nbsp;
Ordered by: standard name
&nbsp;
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)
 ...</pre>
</div>
</div>
<blockquote><p>2706 function calls (2004 primitive calls) in 4.504 CPU seconds</p>
<p>หมายถึง มีการเรียก function 2706 ครั้ง ภายในเวลา 4.504 วินาที สำหรับ primitive calls หมายถึง จำนวนการเรียกฟังก์ชั่นแบบไม่ recursive นั่นคือ ถ้ามีการ recursive primitive จะนับแค่ 1 ครั้งเท่านั้น</p></blockquote>
<blockquote><p>ncalls หมายถึง จำนวนครั้งที่เรียก function นั้นๆๆ</p>
<p>tottime หมายถึง ปริมาณเวลาทั้งหมด ที่ใช้ในการเรียก function ทั้งหมด</p>
<p>percall หมายถึง สัดส่วน ncall/tottime</p>
<p>cumtime (cumulative time) เวลาที่ใช้ใน function นั้นๆ</p>
<p>percall หมายถึง สัดส่วน primitive call/cumulative time</p>
<pre>filename:lineno(function) ระบุ ชื่อไฟล์ บรรทัดที่? และชื่อ function นั้นๆ</pre>
</blockquote>
<p>สำหรับรายละเอียดเพิ่มเติมสามารถดูได้จาก <a href="http://docs.python.org/library/profile.html">http://docs.python.org/library/profile.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.klainfo.com/2010/01/05/identify-the-bottlenecks-by-python-profilers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: www.klainfo.com @ 2012-02-08 07:50:38 -->
