誰是速度之王(Python vs JavaScript)?

Python 是個比較成熟的語言,運行速度在幾年前是快於 JavaScript 的。但這些年  
JavaScript 的解釋器發展很快,特別是 Google 的 V8 和 Mozilla 的 SpiderMonkey,  
將 JavaScript 的運行速度提高了一大塊,以至 JavaScript 的運行速度大有反超 Python  
之勢,但 Python 也不甘示弱,PyPy 項目通過幾年的開發以後,最近也在頻頻發佈版本,  
將 JIT 帶到 Python 之中,因此誰比誰牛,還很難說。這裏作個簡單的測試:  

測試環境:  
CPU:            Intel(R) Pentium(R) CPU G620 @ 2.60GHz 雙核  
操做系統:       Debian GNU/Linux 6.0 64 位  
Google JS 引擎: 來自 Node.js v0.6.12 (命令:node)  
Mozilla JS 引擎:來自 xulrunner-11.0 (命令:xpcshell)  
Python:         Debian 自帶的 Python 2.6.6 (命令:python)  
PyPy:           pypy-1.8 (命令:pypy)  

先測試簡單的循環累加,測試代碼:   node

testSum.js
var i, j, s;

for (i = 0; i < 100; i++) {
    s = 0;
    for (j = 0; j < 100000; j++)
        s += j;
}

if ('function' == typeof(print))
    print(i, s);
else
    console.log(i, s);

testSum.pypython

i = 0
while i < 100:
    s = 0
    j = 0
    while j < 100000:
        s += j
        j += 1
    i += 1

print i, s

測試結果:
time node testSum.js     : 0m0.052s
time xpcshell testSum.js : 0m1.808s
time python testSum.py   : 0m2.199s
web

time pypy testSum.py     : 0m0.188sshell


再測試 Dict 操做,測試代碼:
app

testDict.jside

var D = {};

var i, s, k;

for (i = 0; i < 100000; i++)
    D['k' + i] = i;

for (i = 0; i < 100; i++) {
    s = 0;
    for (k in D)
        s += D[k];
}

if ('function' == typeof(print))
    print(i, s);
else
    console.log(i, s);

testDict.py性能

D = {}

i = 0
while i < 100000:
    D['k%d' % i] = i
    i += 1

i = 0
while i < 100:
    s = 0
    for k in D:
        s += D[k]
    i += 1

print i, s

測試結果: 
time node testDict.js     : 0m3.645s 
time xpcshell testDict.js : 0m2.894s 
time python testDict.py   : 0m2.954s 
time pypy testDict.py     : 0m1.044s 
測試

繼續測,測試 List 操做,測試代碼: spa

testList.js操作系統

var L = [];

var i, k;

for (i = 0; i < 100000; i++)
    L.push('k' + i);

for (i = 0; i < 100000; i++) {
    if (i & 1) {
        k = L.shift();
        L.splice(i, 0, k);
    } else {
        k = L.splice(i, 1)[0];
        L.push(k);
    }
}

if ('function' == typeof(print))
    print(i, L.length);
else
    console.log(i, L.length);

testList.py

L = []

i = 0
while i < 100000:
    L.append('k%d' % i)
    i += 1

i = 0
while i < 100000:
    if i & 1:
        k = L[0]
        del L[0]
        L.insert(i, k)
    else:
        k = L[i]
        del L[i]
        L.append(k)
    i += 1

print i, len(L)

測試結果:
time node testList.js     : 0m5.277s
time xpcshell testList.js : 3m10.457s
time python testList.py   : 0m6.710s
time pypy testList.py     : 0m41.702s

測試總結:
循環累加操做:node     < pypy     < xpcshell  < python
              0m0.052s   0m0.188s   0m1.808s    0m2.199s

Dict 操做:   pypy     < xpcshell < python    < node
              0m1.044s   0m2.894s   0m2.954s    0m3.645s

List 操做:   node     < python   < pypy      < xpcshell
              0m5.277s   0m6.710s   0m41.702s   3m10.457s

    通過簡單的測試代表:
    Node.js 作單純的運算無疑是最快的,但 Node.js 的 Dict 操做性能太差,List 操
做卻又是表現最好的。
    xpcshell 作單純的運算慢於 Node.js,作 Dict 運算快於 Node.js,但作 List 操做
倒是慢的離譜,是最慢的。
    python 作單純的運算是最慢的,Dict 操做表現還不錯,List 操做表現也不錯,我的
感受只要不是作大量的計算,仍是比較適合實現複雜的業務邏輯的。
    PyPy 作單純的計算表現不俗,作 Dict 操做性能最好,可是作 List 操做還不如傳統
的 Python,我的感受 PyPy 頗有前途,但在某些地方或許還不夠成熟。能夠考慮在某些環
境代替標準的 Python,畢竟二者是很是兼容的,能夠互相替換。

    最後的總結:
    Node.js 在適合的環境會表現的很好,但不要過分使用。
    Python 做爲一門成熟的語言,不容忽視,在複雜的環境下,或許 Python 也是好的選
擇。
    做爲語言的使用者,咱們能夠多瞭解幾門語言,瞭解各自的優缺點,在適合的地方,
將適合的語言派上用場。

相關文章
相關標籤/搜索