您的位置:首页 > 编程语言 > Python开发

python 多线程 - Cpython, Jython 和 IronPython的多线程性能初步比较

2012-12-17 07:43 531 查看
(免责声明:本例并不一定典型,请勿上纲上线)

写了个简单的代码想比较下哪个解释器的多线程执行效率比较高……代码如下:

 

'''
Created on Dec 13, 2012

@author: festony
'''

import random
import threading
from cj_lib import *

def test(repeat):
for i in range(repeat):
random.random()

@rec_proc_time('single thread')
def run_test_1():
test(5000000)

@rec_proc_time('multi thread - threading')
def run_test_2():
ths = []
for i in range(10):
ths.append(threading.Thread(target=test,args=(500000,)))
for i in range(10):
ths[i].start()
for i in range(10):
ths[i].join()

run_test_1()
run_test_2()


分别使用Cpython, Jython和IronPython解释执行,结果如下:

CPython:

Function {single thread} process time: 0.74492 sec(s)
Function {multi thread - threading} process time: 1.34382 sec(s)


 

Jython:

Function {single thread} process time: 3.05519 sec(s)
Function {multi thread - threading} process time: 1.37505 sec(s)


 

IronPython:

Function {single thread} process time: 1.48341 sec(s)
Function {multi thread - threading} process time: 1.50126 sec(s)


 

无语啊无语,除了C语言V5我还能说什么呢?

也许该试试python3.3?

 

然后把test函数的重复次数提升到50000000/5000000(十倍),结果:

CPython:

Function {single thread} process time: 7.57535 sec(s)
Function {multi thread - threading} process time: 12.9685 sec(s)


 

Jython:

java.lang.OutOfMemoryError: Java heap space

at org.python.core.__builtin__.range(__builtin__.java:882)


 

(…………)

 

IronPython:

Function {single thread} process time: 14.9009 sec(s)
Function {multi thread - threading} process time: 15.9274 sec(s)


 

 

也许各个解释器的random实现方法不一致?我应该换个函数试试。

 

于是修改了test()中的内容,如下:

ti = [5, 4, 9, 7, 12, 4, 7, 8, 13, 21]

def test(repeat):
for i in range(repeat):
for j in range(10):
x = j * ti[j]


 

把重复次数设为5000000/500000,结果如下:

CPython:

Function {single thread} process time: 6.19796 sec(s)
Function {multi thread - threading} process time: 14.9035 sec(s)


 

Jython:

Function {single thread} process time: 21.2629 sec(s)
Function {multi thread - threading} process time: 52.7627 sec(s)


 

IronPython:

Function {single thread} process time: 3.61389 sec(s)
Function {multi thread - threading} process time: 1.49704 sec(s)


 

嗯……………………
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: