先測試簡單的循環累加,測試代碼: node
testSum.jsvar 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 也是好的選
擇。
做爲語言的使用者,咱們能夠多瞭解幾門語言,瞭解各自的優缺點,在適合的地方,
將適合的語言派上用場。