顯示具有 Programming 標籤的文章。 顯示所有文章
顯示具有 Programming 標籤的文章。 顯示所有文章

2014年9月18日 星期四

Install Flask on CentOS 7.0

1. Install pip on CentOS 7.0

## First install the EPEL repo as per your Operating system version and architecture.
rpm -ivh http://mirror01.idc.hinet.net/EPEL/7/x86_64/e/epel-release-7-1.noarch.rpm
yum install -y python-pip

2. Install Flask

pip install Flask

## Test
vi hello.py

from flask import Flaskapp = Flask(__name__)
@app.route("/")def hello():    return "Hello World!"
if __name__ == "__main__":    app.run()

## Verify
python hello.py

* Running on http://0.0.0.0:80/

2014年7月17日 星期四

Python: ConfigParser

[core]
cmd = /usr/bin/vim

[mod]
safe_edit = yes
time_interval = 20

python 程式如下:
#!/usr/bin/python
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('test.conf')

#傳回字串 /usr/bin/vim
parser.get('core', 'cmd')

#傳回 boolean 值 True
parser.getboolean('mod', 'safe_edit')

#傳回整數值 20
parser.getint('mod', 'time_interval')


references:
http://williewu.blogspot.tw/2007/10/python-configparser.html
https://wiki.python.org/moin/ConfigParserExamples
https://docs.python.org/2.6/library/configparser.html

How to find script's directory

import os
print os.path.dirname(os.path.realpath(__file__))

reference:
http://stackoverflow.com/questions/4934806/python-how-to-find-scripts-directory

2013年7月30日 星期二

GDB: Missing separate debuginfos

Fixed GDB: Missing separate debuginfos

1. 修改 /etc/yum.repos.d/CentOS-Debuginfo.repo, set enabled=1
2. yum install yum-utils
3. debuginfo-install glibc

如果還是不行更新 gdb and glibc

2012年10月17日 星期三

Translate int to string

#include <iostream>
#include <string>
#include <sstream>

using namespace std;
string int2str(int &);
int main(void) {
  int i = 123;
  string s;
  s = int2str(i);

  cout << s << endl;
}


string int2str(int &i) {
  string s;
  stringstream ss(s);
  ss << i;

  return ss.str();
}

2012年10月16日 星期二

判斷奇偶數

利用位元運算來判斷奇偶數

if( (value & 1) == 0 )
  cout << "偶數";
else
  cout << "奇數";

2011年9月26日 星期一

英文標點符號

英文標點符號

.
句號
period
,
逗號
comma
;
分號
semicolon
:
冒號
colon
?
問號
question mark
!
驚嘆號
exclamation point
“ ”
雙引號
double quotation mark
‘ ’
單引號
single quotation mark
–
連字號
hyphen
––
破折號
dash
( )
括號
parenthesis
[ ]
方括號
square bracket
...
刪節號
ellipsis
/
斜線
slash
`
省略號
apostrophe

2011年9月22日 星期四

wxPython

## Install wxPython in CentOS 5.5

[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
enabled=1
yum install wxPython.x86_64 wxPython-devel.x86_64

[references]
wxPython API

wxPyWiki
http://wiki.wxpython.org/FrontPage
http://wiki.wxpython.org/AnotherTutorial

LDS株式會社 blog
The wxPython tutorial
宅學習 python blog
Image Processing in Python
wxPython自製Control : 用Matplotlib畫圖表的FigurePanel
wxPython Programming Tutorial

http://wiki.pastwind.org/home/python-cheng-shi-yu-yan
http://garywlee.blogspot.com/2007/10/wxpython.html

2011年9月21日 星期三

Using Parallelism in Python

1. Cython (C-Extensions for Python)
http://cython.org/
wget http://cython.org/release/Cython-0.15.tar.gz
tar zxf Cython-0.15.tar.gz
cd Cython-0.15
python setup.py install

2. Multi-threading (built-in)
import threading

3. Parallel Python
http://www.parallelpython.com/content/view/17/31/

4. python-multiprocessing
Python 2.5/2.4 back port of the multiprocessing package
http://code.google.com/p/python-multiprocessing/

[references]
Does python support multiprocessor/multicore programming?
Practical threaded programming with Python
Basic Threading in Python
Understanding Threading in Python
Simple Python: a job queue with threading

http://hungic.blogspot.com/2008/08/pythonthreading.html
http://www.cppblog.com/riverbird/archive/2007/12/26/39704.html

2011年7月29日 星期五

How to get the CPU core number

Use sched_getcpu to determine the CPU on which the calling thread is running. See man getcpu (the system call) and man sched_getcpu (a library wrapper).

example:

#include <stdio.h>
#include <utmpx.h>
int main()
{
int c, s;
int cpu;
cpu = sched_getcpu();
printf("id=%d\n", cpu);
return 0;
}

Ps. linux version > 2.6.19

references:
How can I get the CPU core number from within a user-space app (Linux, C)?
INTEL DOC

2011年7月26日 星期二

轉載: Java與C++的比較

--------------------------------------------------------------------------------

作者 tinlans.bbs@whshs.cs.nccu.edu.tw (汀), 看板 Language
標題 Re: 請問為什麼 Java 比 C++ 更熱門?
時間 政大狂狷年少 (Thu Apr 14 04:44:10 2005)
來源 SimFarm!zoonews.ee.ntu!bbs.ee.ntu!news.ntu!Spring!ctu-peer!news.nctu!n
───────────────────────────────────────

※ 引述《HZYSoft.bbs@ptt.cc (PCMan 2004)》之銘言:
> http://kano.net/javabench/ 這篇就是在講 java 可以比 C++ 快的狀況
> 另外網路上還有非常多類似的 benchmark,理論不是絕對正確,有時應該讓數字說話
> 我以前看到一篇很完整的 benchmark,現在已經找不到了。
這篇很久之前我就讀過了,但是相當遺憾的,
那篇有非常非常多的問題在,相信你應該逛過它的 revisited 網站,
也看過 http://www.freewebs.com/godaves/javabench_revisited/ 這篇。
> 我自己是 C++ 的愛用者,沒在寫 java,不過我相信 java 確實不一定會比較慢
> C++ 不應該被神化,在很多地方他的效能確實不好,在一些應用上不見得強過 java
> 這個議題是相當具有爭議性的,ACM 上面也有論文在探討這個,可惜我看不到。
> 網路上充滿大量 benchmark,結論卻是很兩極化。身為 C++ 愛用者我們不能只看到
我們先來複習一下「Computer Architecture」課本,
benchmark 的準確性在 Computer Architecture: A Quantitative Approach 一書中,
被依照預測準確性由高至低分為五個層級:
1. real applications
2. modified (or scripted) applications
3. kernels
4. toy benchmarks
5. synthetic benchmarks
您所提及的網站中所列的 benchmark,大多位於第五個層級。

2011年7月5日 星期二

備份 putty的設定

要備份及回復Putty的設定,只要使用下面兩個指令就可以了。

備份設定請用:

> regedit.exe /e PuTTY.reg HKEY_CURRENT_USER\Software\SimonTatham

回復設定,請用:

> regedit.exe /i Putty.reg

其中,Putty.reg就是Putty的設定資訊,您可以將它複製到不同的電腦,再以同樣的回復步驟就可以有一樣的設定了。

2011年6月9日 星期四

gprof 看程式效能分析

gcc 加參數看程式的performance 分析

compiler 時加參數 -pg
gcc -pg test.c
./a.out

然後, 執行程式後會產生出gmon.out

gprof a.out > test
去看 test 檔, 可以看到很多有用的 performance analysis 的資訊

subroutine 執行幾次, 被那些呼叫, 佔執行時間多少, 等等..

References:

使用Gnu gprof進行Linux平臺下的程序分析
http://dipeng.blog.hexun.com.tw/12501019_d.html

使用Gnu gprof進行Linux平台下的程序分析
http://southmaster.com/article/pub.php?page=red2.php&id=3255

資料型態 DataType

程 式在執行的過程中,需要運算許多的資訊,也需要儲存許多的資訊,資訊是儲存在記憶體空間中,由於資料的型態各不相同,在儲存時所需要的容量不一,不同的 資料必須要配給不同的空間大小來儲存,因而有了「資料型態」(Data type)的規範。

C++中基本的資料型態主要區分為「整數」(Integer)、「浮點數」(Float)、「字元」(Character),而這幾種還可以細分,如下所 示:
整數
用 來表示整數值,可以區分為short、int、與long,可容納的大小各不相同,short的長度為半個word,int表示一個word,而long 可能是一個或兩個word,在32位元機器上int與long的長度通常是相同的,型態的長度越長,表示可表示的整數值範圍越大。
浮點數
用 來表示小數值,可以區分為float、double與long double,float的長度為一個word,double的長度為二個word,long double長度為3或4個word。
字元
用 來儲存字元,長度為1個位元組,其字元編碼主要依ASCII表而來,由於字元在記憶體中所佔有的空間較小,所以它也可以用來儲存較小範圍的整數。


以上的資料型態在記憶體中所佔有的大小依平台系統而有所差異,word的大小取決於機器,在32位元機器上通常一個word是4個位元組,如果想要知道這 些資 料型態在您所使用的平台上,所佔有的記憶體空間有多少,最好的作法是使用sizeof()運算子,它可以告訴您確實的記憶體大小,下面這個程式是個簡單的示範(64 bit):


Data type       (bytes)
int     4
long    8
float   4
double  8
char    1

參考資料
http://en.wikipedia.org/wiki/C_data_types
http://en.wikipedia.org/wiki/Integer_(computer_science)
http://caterpillar.onlyfun.net/Gossip/CppGossip/DataType.html
C code 與 assembly output 交叉出現

2011年5月31日 星期二

C floor

a=0.3/0.0125;
printf("a1=%f\n", a);
printf("floor(a1)=%g\n", floorf(a));

a=floorf(0.3/0.0125);
printf("a1=%f\n", a);


printf("%f\n", floor((0.3/0.0125)));


a1=24.000000
floor(a1)=24
a1=24.000000
23.000000


在C 和Fortran 得到的結果不一樣, 原來在C 語言中要用floorf!! XD

補充

ceil(x) 返回不小于x的最小整数值 (天花板)
floor(x) 返回不大于x的最大整数值 (地板)
round(x) 返回x的四舍五入整数值

2011年5月4日 星期三

global variable in python

在Python 中修改到全域變數時要宣告global


globvar = 0
def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1
def print_globvar():
    print globvar     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()       # Prints 1

2011年4月22日 星期五

"SyntaxError: invalid syntax" with python 2.4

在python 2.4 使用try-except-finally 時出現(如下), 是因為要到python 2.5 時才完全支援try-except-finally


File "try-except.py", line 13
    finally:
          ^
SyntaxError: invalid syntax

in python 2.4 可以改用

try:
    try:
    except:
finally:

2011年1月25日 星期二

常用的 Python指令及其說明

常用的 Python指令及其說明 [轉載: 我是孩子王

sys模組:

argv: 命令列參數。
exit([arg]): 結束程式。
exitfunc: 若有指定此函式,程式結束前會先呼叫此函式。

getopt模組:

getopt(args, options[, long_options]): 解譯命令列參數選項的工具。

2011年1月18日 星期二

getopt 分析命令行參數

getopt()用來分析命令行參數。參數argc和argv是由main()傳遞的參數個數和內容。參數optstring 則代表欲處理的選項字符串。此函數會返回在argv 中下一個的選項字母,此字母會對應參數optstring 中的字母。如果選項字符串裡的字母后接著冒號“:”,則表示還有相關的參數,全域變量optarg 即會指向此額外參數。如果getopt()找不到符合的參數則會印出錯信息,並將全域變量optopt設為“?”字符,如果不希望getopt()印出錯信息,則只要將全域變量opterr設為0即可。

http://www.gnu.org/s/libc/manual/html_node/Getopt.html

2011年1月3日 星期一

cout and printf in OpenMP

Many implementations of printf acquire a lock to ensure that each printf call is not interrupted by other threads.

In contrast, std::cout's overloaded << operator means that (even with a lock) one thread's printing of i and ' ' and '\n' can be interleaved with another thread's output, because std::cout << i << " " << endl; is translated to three operator<<() function calls by the C++ compiler.

[References]
http://stackoverflow.com/questions/4459888/openmp-c-and-c-cout-printf-does-not-give-the-same-output
http://berenger.eu/blog/2010/12/06/c-openmp-stdcout-print-with-openmp/