谁是速度之王(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 也是好的选
择。
    做为语言的使用者,咱们能够多了解几门语言,了解各自的优缺点,在适合的地方,
将适合的语言派上用场。

相关文章
相关标签/搜索