Chinaunix

标题: python和php的执行速度哪个快一些 [打印本页]

作者: yahoo21cn    时间: 2006-04-15 15:56
标题: python和php的执行速度哪个快一些
程序内对数据运算处理的速度,不设计数据库和文件操作。纯处理速度。
作者: yahoo21cn    时间: 2006-04-15 16:47
奇怪,整数运算python比php慢一倍,大家看看是不是我写的有问题,初学python,有可能是我些错了造成的。

php:
<?
$n=1;
$time=time();
while($n<100000000){
        $n=$n+1;
}
echo time();
echo "<br>";
echo $time;
echo "<br>";
echo time()-$time;
?>


python:
# -*- coding:gbk -*-
import time
n=1
time_start=time.clock()

while n<100000000:
    n=n+1

time_end=time.clock()
print time_end-time_start




php用时25秒,python是50秒。
作者: loveddie    时间: 2006-04-15 20:21
比较似乎意义不大.

  1. import time

  2. class timer:
  3.     def __init__(self):
  4.         self.start= time.time()
  5.     def stop(self):
  6.         self.end= time.time()
  7.         return  "with %f seconds"% (self.end-self.start)

  8. clock=timer()
  9. n=1
  10. while n<100000000:
  11.     n+=1

  12. print clock.stop()
  13. print n
复制代码


with 31.234000 seconds
100000000


  1. #include <stdio.h>
  2. #include <time.h>

  3. void main()
  4. {
  5.                 clock_t  start,end;
  6.         start=clock();
  7.         long int n=0;
  8.         while(n<100000000)
  9.                 n+=1;
  10.         end=clock();
  11.         printf("with %f seconds\n",(double)(end-start)/CLK_TCK);
  12.         printf("%ld\n",n);
  13. }
复制代码


with 0.421000 seconds
100000000

嘿嘿,php还是没C快吧. 尽管python......, 还是喜欢python.
作者: yahoo21cn    时间: 2006-04-15 22:12
有必要学c了
作者: mcyclone    时间: 2006-05-17 16:17
写出来的代码很简洁呢  关注一下
作者: bleem1998    时间: 2006-05-17 17:16
python慢的出奇
在差一点的CPU上尤其明显
作者: 星尘细雨    时间: 2006-05-17 17:42
你是哪个版本的python?
在什么系统上运行的?
作者: bleem1998    时间: 2006-05-17 18:12
原帖由 星尘细雨 于 2006-5-17 17:42 发表
你是哪个版本的python?
在什么系统上运行的?


python2.4
在ARM9的CPU上
很早以前的事
现在没环境干这种坏事了
作者: newbuding    时间: 2006-05-17 22:41
python还可以做一些优化啊,比方说预编译成.pyc文件
还有就是现在的计算机,速度基本上已经不是什么问题,主要是看你的算法是否恰当合理
推荐楼主看看《一切从游戏开始》这篇文章
作者: 星尘细雨    时间: 2006-05-18 10:02
原帖由 bleem1998 于 2006-5-17 18:12 发表


python2.4
在ARM9的CPU上
很早以前的事
现在没环境干这种坏事了


哦,在linux上python运行的比windows的快。
作者: tigerpower    时间: 2006-05-18 19:42
python慢是出了名的,肯定比perl慢,但偶还是喜欢用python。
作者: 蒸汽    时间: 2006-05-19 10:19
我觉得,你把PYTHON里的算时间的写成函数,而C是过程.
2种方式可能会有些速度差距,比较就应该写成一模一样的过程
作者: meud    时间: 2006-06-02 13:18
标题: PY终极加速!!!
  1. import time
  2. try:
  3.     import psyco
  4.     psyco.full()
  5. except ImportError:
  6.     print 'no mod psyco'

  7. def test():
  8.     n = 1
  9.     for i in xrange( 100000000 ):
  10.         n += 1

  11. start = time.clock()
  12. test()
  13. end = time.clock() - start
  14. print '%.8f' % end
复制代码


执行结果:0.18338392
大家可以装一下Psyco模块
然后比较一下优化后的结果
作者: meud    时间: 2006-06-02 13:30
未优化:9.90517175

使用优化后,速度提高了五十四倍

对于字符串处理速度更是明显,在我的测试中甚至达到了一百倍的加速效果。
以下是字符串操作测试代码:
  1. # -*- coding:UTF-8 -*-
  2. import time
  3. try:
  4.     import psyco
  5.     psyco.full()
  6. except ImportError:
  7.     print 'error'

  8. def test( s ):
  9.     t = 0
  10.     for i in xrange( len( s ) ):
  11.         if s[i] == 'a':
  12.             t += 1
  13.     return t

  14. s = '中华人民共和国a中华人民共和国' * 1024
  15. test( s )
  16. start = time.clock()
  17. for i in xrange( 1000 ):
  18.     e = test( s )
  19. end = time.clock()
  20. print e, 'time:%.6f' % ( end - start ), 'speed:%.6f M/s' % ( len( s )*1000/( end - start )/1024/1024 )
复制代码

作者: baif    时间: 2006-06-02 15:18
  1. Psyco is a Python extension module which can massively speed up the execution of any Python code.
复制代码

作者: ipaddr    时间: 2006-06-08 14:55
PHP也有ZEND可以优化.
作者: lzhome    时间: 2006-06-08 16:17
psyco !强啊!我应用到我的程序里速度果然成倍提高!meud,拜你!
作者: fxsjy    时间: 2006-06-11 11:41
为什么在自己的PC上优化很明显,可是在服务器上用psyco后,速度反而下降了?
服务器2个cpu,1G内存。是不是硬件体系结构不同造成的啊?
作者: newbuding    时间: 2006-06-11 21:20
你在服务器上跑什么应用?
作者: baif    时间: 2006-06-12 16:14
原帖由 fxsjy 于 2006-6-11 11:41 发表
为什么在自己的PC上优化很明显,可是在服务器上用psyco后,速度反而下降了?
服务器2个cpu,1G内存。是不是硬件体系结构不同造成的啊?


是如何统计的啊? psyco早已经停止开发了。。。。而且还不支持64-bit的构架
作者: guotie    时间: 2006-06-12 17:26
差别的确很大:
php:39
py:85.74
c:可以忽略
作者: ghostwwl    时间: 2006-06-12 19:13
不久的将来psyco将被纳入 python的标准库
作者: assiss    时间: 2006-06-13 07:55
原帖由 ghostwwl 于 2006-6-12 19:13 发表
不久的将来psyco将被纳入 python的标准库

在哪里看到的?
目前仅支持I386体系的PSYCO,恐怕很难加入到标准库里。
作者: guotie    时间: 2006-06-13 15:35
不是说psyco停止开发了么
作者: fxsjy    时间: 2006-06-14 01:22
用的程序就是个循环计数,还有一个传输文件的。
作者: Aryang    时间: 2006-06-15 11:08
psyco早已经停止开发了??
我去它的主页上看,还有 24 March 2006 的news
作者: guotie    时间: 2006-06-15 11:13
各种语言的评测:

http://shootout.alioth.debian.org/sandbox/

综合来看,各语言得分如下:

ratio         language         score         ×
        best possible        100.0         
1.0        C gcc         69.5        1
1.0        D Digital Mars         69.2         
1.2        C++ g++         56.2        2
1.3        Eiffel SmartEiffel         52.6        2
1.5        SML MLton         46.1        2
1.5        OCaml         45.0        1
1.6        Fortran Intel         44.8        3
1.6        Clean         43.1        7
1.8        Haskell GHC         38.4        1
1.8        Ada 95 GNAT         38.4        1
2.1        C Tiny         32.8        7
2.2        Oberon-2 OO2C         31.4        7
2.2        Pascal Free Pascal         31.3        4
2.3        Lisp SBCL         30.6        4
2.3        Java JDK -server         30.6        1
2.4        Java JDK -client         29.0         
2.5        Java GCJ         27.5        3
2.8        Fortran G95         25.0        5
3.1        C# Mono         22.4        1
3.2        Nice         21.4        5
3.4        Dylan Gwydion         20.2        7
3.5        Java JDK 1.4 -server         19.9        7
3.8        C Cyclone         18.3        10
4.4        SML SML/NJ         15.9        10
4.5        Python Psyco         15.5         
5.0        Objective-C         14.0        13
5.4        OCaml (bytecode)         13.0        1
6.0        Parrot PIR         11.5        5
6.0        Forth GForth         11.5        1
6.6        Lisp CMUCL         10.6        14
7.9        Lua         8.8         
8.7        Python         8.0        1
9.4        Erlang HiPE         7.4        4
9.9        Tcl         7.0        3
11        Oberon-2 XDS         6.2        13
11        Pike         6.1        5
12        Erlang         5.9        4
13        Perl         5.2        4
14        Scheme Chicken         5.0        7
15        Scheme MzScheme         4.6        5
15        Scheme Bigloo         4.6        11
18        Java JDK -Xint         3.9        1
20        Ruby         3.5        2
22        S-Lang         3.1        8
24        Smalltalk GST         2.9        2
26        Icon         2.7        8
29        PHP         2.4        8
40        Python IronPython         1.7        6
54        Mozart/Oz         1.3        6
130        Io         0.5        12
189        JavaScript SpiderMonkey         0.4        9
223        Rexx Regina         0.3        12
435        Lisp Newlisp         0.2        16
        Java SableVM         0.0        17
作者: newbuding    时间: 2006-06-16 08:02
这分怎么算的?都很低啊?
作者: newbuding    时间: 2006-06-16 08:04
标题: 哈哈,看看最新最全的表

  1. ratio         language         score         ×
  2.         best possible        300.0         
  3. 1.0        C gcc         166.1        1
  4. 1.1        D Digital Mars         156.6         
  5. 1.2        Haskell GHC         136.3        1
  6. 1.2        OCaml         135.1        1
  7. 1.3        C++ g++         130.0        2
  8. 1.3        SML MLton         125.7        2
  9. 1.4        Pascal Free Pascal         118.8        4
  10. 1.4        Eiffel SmartEiffel         116.5        2
  11. 1.7        OCaml (bytecode)         100.0        1
  12. 1.7        Forth GForth         99.9        1
  13. 1.8        Lua         94.4         
  14. 1.8        Clean         90.8        7
  15. 1.9        Python         87.0        1
  16. 1.9        C Tiny         86.0        7
  17. 1.9        Fortran Intel         85.8        3
  18. 2.0        Ada 95 GNAT         85.2        1
  19. 2.0        Fortran G95         83.1        5
  20. 2.0        Python Psyco         82.2         
  21. 2.2        Java JDK -client         77.2         
  22. 2.2        Java JDK -server         76.4        1
  23. 2.2        Tcl         75.1        3
  24. 2.3        Ruby         71.1        2
  25. 2.4        Perl         70.5        4
  26. 2.4        C# Mono         69.8        1
  27. 2.4        Lisp SBCL         69.6        4
  28. 2.5        Nice         65.6        5
  29. 2.6        Java GCJ         64.4        3
  30. 2.7        Oberon-2 OO2C         61.1        7
  31. 3.0        Pike         55.6        5
  32. 3.1        Python IronPython         53.1        6
  33. 3.2        Java JDK -Xint         51.6        1
  34. 3.3        Dylan Gwydion         50.4        7
  35. 3.3        Erlang HiPE         49.9        4
  36. 3.3        Java JDK 1.4 -server         49.7        7
  37. 3.4        Erlang         48.5        4
  38. 3.5        C Cyclone         48.0        10
  39. 3.8        Scheme MzScheme         43.2        5
  40. 3.9        Smalltalk GST         42.4        2
  41. 4.0        S-Lang         41.1        8
  42. 4.1        Scheme Chicken         40.8        7
  43. 4.1        Icon         40.1        8
  44. 4.3        SML SML/NJ         38.8        10
  45. 4.5        Objective-C         37.0        13
  46. 4.5        JavaScript SpiderMonkey         36.6        9
  47. 5.5        PHP         30.4        8
  48. 5.7        Rexx Regina         29.2        12
  49. 6.1        Scheme Bigloo         27.3        11
  50. 6.2        Oberon-2 XDS         26.6        13
  51. 6.6        Parrot PIR         25.3        5
  52. 7.4        Lisp CMUCL         22.4        14
  53. 7.9        Mozart/Oz         21.1        6
  54. 14        Lisp Newlisp         11.5        16
  55. 43        Io         3.9        12
  56. 69        Java SableVM         2.4        17
复制代码

作者: equalnull    时间: 2006-07-31 19:17
还是C牛B!!!
一定要学好才行。
作者: lzhome    时间: 2006-08-01 11:33
都看仔细了!前面是语言,后面是编译器! c用gcc,c++用g++,而python居然用.NET平台的ironpython解析,根本没有可比性。如果python用unix下解析,相信分数不会这么低的!
作者: eha    时间: 2006-08-02 10:03
提示: 作者被禁止或删除 内容自动屏蔽




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2