Python速度很快(是:Python的速度有多快) [英] Python is darn fast (was: How fast is Python)

查看:86
本文介绍了Python速度很快(是:Python的速度有多快)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个星期前我发布了这个帖子(还记得C Sharp的帖子吗?)但是在大量的帖子中没有注意到它是b / b
,所以让我重试一下。在这里,我获得了Python +

Psyco的速度是优化C的两倍,所以我想现在如果我的旧笔记本电脑上有什么东西是错误的,那么任何人都可以重现我的结果。

这是我用于调用错误函数一百万次的数字

(Python 2.3,Psyco 1.0,Red Hat Linux 7.3,Pentium II 366 MHz):


$时间p23 erf.py

实际0m0.614s

用户0m0.551s

sys 0m0。 029s


这是优化C的两倍:


$ gcc erf.c -lm -o3

$ time ./a.out

real 0m1.125s

用户0m1.086s

sys 0m0.006s


以下是纯Python的情况


$ time p23 erf.jy

real 0m25.761s

用户0m25.012s

系统0m0.049s

,只是为了好玩,这里是Jython性能:


$ time jython erf.jy

real 0m42.979s

用户0m41.430s

sys 0m0.361s


源代码如下(复制自Alex Martelli的帖子):


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


$ cat erf.py

导入数学

导入psyco

psyco.full()

def erfc(x):

exp = math.exp


p = 0.3275911

a1 = 0.254829592

a2 = -0.284496736

a3 = 1.421413741

a4 = -1.453152027

a5 = 1.061405429

t = 1.0 /(1.0 + p * x)

erfcx =((a1 +(a2 +(a3 +

(a4 + a5 * t)* t)* t)* t)* t)* exp(-x * x)

返回erfcx


def main():

erg = 0.0


for i在xrange(1000000):

erg + = erfc(0.456)


if __name__ ==''__ main__'':

main()


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


#py thon / jython版本=相同没有import psyco; psyco.full()"


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

$ cat erf.c

#include< stdio.h>

#include< math.h>


双倍erfc(双x)

{

双倍p,a1,a2,a3,a4,a5;

双t ,erfcx;


p = 0.3275911;

a1 = 0.254829592;

a2 = -0.284496736;

a3 = 1.421413741;

a4 = -1.453152027;

a5 = 1.061405429;


t = 1.0 /(1.0 + p * x);

erfcx =((a1 +(a2 +(a3 +

(a4 + a5 * t)* t)* t)* t)* t) * exp(-x * x);


返回erfcx;

}


int main()

{

double erg = 0.0;

int i;


for(i = 0; i< 1000000; i ++)

{

erg = erg + erfc(0.456);

}


返回0;

}


Michele Simionato,博士。
Mi ************** @ libero.it
http://www.phyast.pitt.edu/~micheles

---目前正在找工作---

I posted this few weeks ago (remember the C Sharp thread?) but it went
unnoticed on the large mass of posts, so let me retry. Here I get Python+
Psyco twice as fast as optimized C, so I would like to now if something
is wrong on my old laptop and if anybody can reproduce my results.
Here are I my numbers for calling the error function a million times
(Python 2.3, Psyco 1.0, Red Hat Linux 7.3, Pentium II 366 MHz):

$ time p23 erf.py
real 0m0.614s
user 0m0.551s
sys 0m0.029s

This is twice as fast as optimized C:

$ gcc erf.c -lm -o3
$ time ./a.out
real 0m1.125s
user 0m1.086s
sys 0m0.006s

Here is the situation for pure Python

$time p23 erf.jy
real 0m25.761s
user 0m25.012s
sys 0m0.049s

and, just for fun, here is Jython performance:

$ time jython erf.jy
real 0m42.979s
user 0m41.430s
sys 0m0.361s

The source code follows (copied from Alex Martelli''s post):

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

$ cat erf.py
import math
import psyco
psyco.full()

def erfc(x):
exp = math.exp

p = 0.3275911
a1 = 0.254829592
a2 = -0.284496736
a3 = 1.421413741
a4 = -1.453152027
a5 = 1.061405429

t = 1.0 / (1.0 + p*x)
erfcx = ( (a1 + (a2 + (a3 +
(a4 + a5*t)*t)*t)*t)*t ) * exp(-x*x)
return erfcx

def main():
erg = 0.0

for i in xrange(1000000):
erg += erfc(0.456)

if __name__ == ''__main__'':
main()

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

# python/jython version = same without "import psyco; psyco.full()"

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

$cat erf.c
#include <stdio.h>
#include <math.h>

double erfc( double x )
{
double p, a1, a2, a3, a4, a5;
double t, erfcx;

p = 0.3275911;
a1 = 0.254829592;
a2 = -0.284496736;
a3 = 1.421413741;
a4 = -1.453152027;
a5 = 1.061405429;

t = 1.0 / (1.0 + p*x);
erfcx = ( (a1 + (a2 + (a3 +
(a4 + a5*t)*t)*t)*t)*t ) * exp(-x*x);

return erfcx;
}

int main()
{
double erg=0.0;
int i;

for(i=0; i<1000000; i++)
{
erg = erg + erfc(0.456);
}

return 0;
}

Michele Simionato, Ph. D.
Mi**************@libero.it
http://www.phyast.pitt.edu/~micheles
--- Currently looking for a job ---

推荐答案

时间p23 erf.py

实际0m0 .614s

用户0m0.551s

sys 0m0.029s


这是优化C的两倍:

time p23 erf.py
real 0m0.614s
user 0m0.551s
sys 0m0.029s

This is twice as fast as optimized C:


gcc erf.c -lm -o3
gcc erf.c -lm -o3


time ./a.out

real 0m1.125s

用户0m1.086s

sys 0m0.006s


以下是纯Python的情况

time ./a.out
real 0m1.125s
user 0m1.086s
sys 0m0.006s

Here is the situation for pure Python


这篇关于Python速度很快(是:Python的速度有多快)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆