Python论坛  - 讨论区

标题:[python-chinese] Ruby1.9运算性能超过Python2.5.1

2007年11月30日 星期五 16:46

junyi sun ccnusjy在gmail.com
星期五 十一月 30 16:46:51 HKT 2007

Ô­ÎÄ£º
http://antoniocangiano.com/2007/11/28/holy-shmoly-ruby-19-smokes-python-away/#comment-1524

²âÊÔÕßʹÓÃûÓÐÓÅ»¯µÄËã·¨ÇófibonacciÊý£¬²âÊÔ½á¹ûÈçÏ£º

*Ruby 1.8.6*:       158.869s
*Python 2.5.1*:      31.507s
*Ruby 1.9.0*:        11.934s
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071130/cd6aab07/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 17:14

doudou doudou array.doudou在gmail.com
星期五 十一月 30 17:14:26 HKT 2007

呵呵,好玩,我们一起来做算法优化吧,每位同学都把自己运行原算法的时间和改进后的程序,以及运行时间贴出来看看。

我先扔个砖头,大家赶紧把玉砸过来吧:

原算法:59.130s
新算法:58.424s
改进:0和1的条件判断改为一项

def fib(n):
    if n<2:
        return n
    else:
        return fib(n-1)+fib(n-2)

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 17:22

junyi sun ccnusjy在gmail.com
星期五 十一月 30 17:22:00 HKT 2007

Ëã·¨ÓÅ»¯²»ÊÇÎÒ·¢ÌûµÄÄ¿µÄ£¬Ô­ÎıȽϵÄÊÇͬÑùËã·¨Çé¿öÏÂÇófibonacciÊýµÄËÙ¶È£¬²¢ÒÔ´ËÀ´±È½ÏpythonºÍrubyµÄ½âÊÍÆ÷µÄÐÔÄÜ¡£

¿ÉÒÔ¿´µ½£¬µ±Ç°£¬ruby1.86Îȶ¨°æ»¹ÊDZÈpython2.5²îºÜ¶àµÄ£¬µ«ÊÇruby1.9²ÉÓÃÁËyarv vm £¬ÒѾ­Ó­Í·¸ÏÉϲ¢³¬¹ýÁË¡£

²»ÖªµÀpython3k³ýÁËÓï·¨ºÍ¿âµÄ¸Ä½øÒÔÍâÓÐûÓÐÔÚ½âÊÍÆ÷µÄÓÅ»¯ÉÏϹ¦·ò£¿



On Nov 30, 2007 5:14 PM, doudou doudou <array.doudou在gmail.com> wrote:

> ºÇºÇ£¬ºÃÍæ£¬ÎÒÃÇÒ»ÆðÀ´×öËã·¨ÓÅ»¯°É£¬Ã¿Î»Í¬Ñ§¶¼°Ñ×Ô¼ºÔËÐÐÔ­Ëã·¨µÄʱ¼äºÍ¸Ä½øºóµÄ³ÌÐò£¬ÒÔ¼°ÔËÐÐʱ¼äÌù³öÀ´¿´¿´¡£
>
> ÎÒÏÈÈÓ¸öשͷ£¬´ó¼Ò¸Ï½ô°ÑÓñÔÒ¹ýÀ´°É£º
>
> Ô­Ëã·¨£º59.130s
> ÐÂËã·¨£º58.424s
> ¸Ä½ø£º0ºÍ1µÄÌõ¼þÅжϸÄΪһÏî
>
> def fib(n):
>    if n<2:
>        return n
>    else:
>        return fib(n-1)+fib(n-2)
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071130/39c618fc/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 17:22

limodou limodou在gmail.com
星期五 十一月 30 17:22:11 HKT 2007

On Nov 30, 2007 5:14 PM, doudou doudou <array.doudou在gmail.com> wrote:
> 呵呵,好玩,我们一起来做算法优化吧,每位同学都把自己运行原算法的时间和改进后的程序,以及运行时间贴出来看看。
>
> 我先扔个砖头,大家赶紧把玉砸过来吧:
>
> 原算法:59.130s
> 新算法:58.424s
> 改进:0和1的条件判断改为一项
>
> def fib(n):
>     if n<2:
>         return n
>     else:
>         return fib(n-1)+fib(n-2)

有人使用generator做了一个,要快多了:

http://www.mysoftparade.com/blog/ruby-19-doesnt-smoke-python-away/

-- 
I like python!
UliPad <>: http://code.google.com/p/ulipad/
meide <>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 17:30

junyi sun ccnusjy在gmail.com
星期五 十一月 30 17:30:20 HKT 2007

¿´ÁËһϣ¬ÎÒ¾õµÃ±¾ÖÊÉÏÊǰѵݹé¸Ä³ÉÁ˵ÝÍÆ£¬ÊôÓÚËã·¨µÄÓÅ»¯£¬ÒòΪÕâÊÇÓÐÖØµþµÄµÝ¹é£¬ËùÒÔËã·¨ÓÅ»¯µÄÓàµØºÜ´ó¡£

On Nov 30, 2007 5:22 PM, limodou <limodou在gmail.com> wrote:

> On Nov 30, 2007 5:14 PM, doudou doudou <array.doudou在gmail.com> wrote:
> > ºÇºÇ£¬ºÃÍæ£¬ÎÒÃÇÒ»ÆðÀ´×öËã·¨ÓÅ»¯°É£¬Ã¿Î»Í¬Ñ§¶¼°Ñ×Ô¼ºÔËÐÐÔ­Ëã·¨µÄʱ¼äºÍ¸Ä½øºóµÄ³ÌÐò£¬ÒÔ¼°ÔËÐÐʱ¼äÌù³öÀ´¿´¿´¡£
> >
> > ÎÒÏÈÈÓ¸öשͷ£¬´ó¼Ò¸Ï½ô°ÑÓñÔÒ¹ýÀ´°É£º
> >
> > Ô­Ëã·¨£º59.130s
> > ÐÂËã·¨£º58.424s
> > ¸Ä½ø£º0ºÍ1µÄÌõ¼þÅжϸÄΪһÏî
> >
> > def fib(n):
> >     if n<2:
> >         return n
> >     else:
> >         return fib(n-1)+fib(n-2)
>
> ÓÐÈËʹÓÃgenerator×öÁËÒ»¸ö£¬Òª¿ì¶àÁË£º
>
> http://www.mysoftparade.com/blog/ruby-19-doesnt-smoke-python-away/
>
> --
> I like python!
> UliPad <>: http://code.google.com/p/ulipad/
> meide <>: http://code.google.com/p/meide/
> My Blog: http://www.donews.net/limodou
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071130/c5e5c7d7/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 18:05

swordsp sparas2006在gmail.com
星期五 十一月 30 18:05:08 HKT 2007

On 11/30/07, doudou doudou <array.doudou at gmail.com> wrote:
> 呵呵,好玩,我们一起来做算法优化吧,每位同学都把自己运行原算法的时间和改进后的程序,以及运行时间贴出来看看。
>
> 我先扔个砖头,大家赶紧把玉砸过来吧:
>
> 原算法:59.130s
> 新算法:58.424s
> 改进:0和1的条件判断改为一项
>
> def fib(n):
>     if n<2:
>         return n
>     else:
>         return fib(n-1)+fib(n-2)

递归 -> 迭代:

def fib_iter(n):
    curr, next = 0, 1
    for i in xrange(n):
        curr, next = next, curr + next
    return curr

复杂度:O(2^n) -> O(n)
时间就不测了。

然而最好的优化始终是数学……

import math
def fib_math(n):
    sqrt5 = math.sqrt(5)
    return int(round( ((sqrt5+1)/2) ** n / sqrt5))

当然,这里直接用python内部实数乘方的实现精度不够,需要找一个专门的数学库。
高精度乘方的复杂度可以做到 O( log(n) ),所以这比迭代法又降了一阶。
如果有足够大的对数表可以查,那么对于一定范围内的n可以当做是常数时间。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 18:14

swordsp sparas2006在gmail.com
星期五 十一月 30 18:14:46 HKT 2007

On 11/30/07, junyi sun <ccnusjy at gmail.com> wrote:
> 算法优化不是我发帖的目的,原文比较的是同样算法情况下求fibonacci数的速度,并以此来比较python和ruby的解释器的性能。
>
> 可以看到,当前,ruby1.86稳定版还是比python2.5差很多的,但是ruby1.9采用了yarv vm ,已经迎头赶上并超过了。
>
> 不知道python3k除了语法和库的改进以外有没有在解释器的优化上下功夫?
>

VM比解释器有先天优势,所以python被超过是迟早的……
目前看来python解释器的实现很长一段时间内不会有什么大的变革,所以python要优化的话突破可能主要在两个方向上:
1. 虚拟机上的python实现,包括jython、ironpython、parrot
2. pypy的jit技术

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 18:15

doudou doudou array.doudou在gmail.com
星期五 十一月 30 18:15:11 HKT 2007

原来的代码经pyrex编译成C代码之后,再编译成Python扩展模块
Python2.4.4
Pentium4 3.2GHz
Debian etch 4.0

原时间59.130秒
现在41.167秒

已经基本等同于C的代码速度了。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 18:27

vicalloy zbirder在gmail.com
星期五 十一月 30 18:27:25 HKT 2007

jython都没什么动静了。JRuby却已是官方的了,JRuby版本的ROR的版本都已突破了1.0。
总体感觉python和.net比较亲。
脚本语言都有自己的局限性,性能一直都不是摆在首要位置的(能快些当然最好了)。

在 07-11-30,swordsp<sparas2006在gmail.com> 写道:
> On 11/30/07, junyi sun <ccnusjy在gmail.com> wrote:
> > 算法优化不是我发帖的目的,原文比较的是同样算法情况下求fibonacci数的速度,并以此来比较python和ruby的解释器的性能。
> >
> > 可以看到,当前,ruby1.86稳定版还是比python2.5差很多的,但是ruby1.9采用了yarv vm ,已经迎头赶上并超过了。
> >
> > 不知道python3k除了语法和库的改进以外有没有在解释器的优化上下功夫?
> >
>
> VM比解释器有先天优势,所以python被超过是迟早的……
> 目前看来python解释器的实现很长一段时间内不会有什么大的变革,所以python要优化的话突破可能主要在两个方向上:
> 1. 虚拟机上的python实现,包括jython、ironpython、parrot
> 2. pypy的jit技术
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese


-- 
Blog http://vicalloy.spaces.live.com/
My googlepage http://vicalloy.googlepages.com/
OldPhoto http://www.lzpian.com/

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 18:28

doudou doudou array.doudou在gmail.com
星期五 十一月 30 18:28:41 HKT 2007

晕,相同的算法,C的时间为0.576s。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 18:30

Albert Lee hanzhupeng在gmail.com
星期五 十一月 30 18:30:08 HKT 2007

试验了下 haskell 和并发的 6.6.1 版本,编译执行,双核

1。 简单版本,无并发
naive_fib 1 = 1
naive_fib 2 = 1
naive_fib n = naive_fib (n-1) + naive_fib (n-2)

main = do
    mapM_ (print . naive_fib) [1..36]

执行:
#time ./fib_no

...

real    0m7.797s
user    0m7.574s
sys     0m0.066s

======================

增加多线程的版本:

编译:  $ ghc-6.6.1 -threaded -O fib.hs -o fib
执行:  $ time ./fib +RTS -N2

real    0m3.665s
user    0m5.176s
sys     0m0.067s

举这个例子只为试验双核的性能优化



On 30/11/2007, doudou doudou <array.doudou在gmail.com> wrote:
> 原来的代码经pyrex编译成C代码之后,再编译成Python扩展模块
> Python2.4.4
> Pentium4 3.2GHz
> Debian etch 4.0
>
> 原时间59.130秒
> 现在41.167秒
>
> 已经基本等同于C的代码速度了。
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 18:31

Albert Lee hanzhupeng在gmail.com
星期五 十一月 30 18:31:57 HKT 2007

忘了贴多线程版本的代码了:


module Main where
import Control.Concurrent
import Control.Concurrent.Chan
import Control.Concurrent.STM
import Control.Exception

naive_fib 1 = 1
naive_fib 2 = 1
naive_fib n = naive_fib (n-1) + naive_fib (n-2)


main = do
    tc <- atomically $ newTVar 2
    mapM_ (\x -> forkIO ((print . naive_fib) x `finally` atomReplace
(\x -> x - 1) tc)) [1..36]
    atomically $ readTVar tc >>= \x -> if x == 0 then return () else retry

atomReplace fn x = atomically $ readTVar x >>= writeTVar x . fn

核心部分就是  forkIO 创建线程。其他的代码只是为了线程同步


On 30/11/2007, Albert Lee <hanzhupeng在gmail.com> wrote:
> 试验了下 haskell 和并发的 6.6.1 版本,编译执行,双核
>
> 1。 简单版本,无并发
> naive_fib 1 = 1
> naive_fib 2 = 1
> naive_fib n = naive_fib (n-1) + naive_fib (n-2)
>
> main = do
>     mapM_ (print . naive_fib) [1..36]
>
> 执行:
> #time ./fib_no
>
> ...
>
> real    0m7.797s
> user    0m7.574s
> sys     0m0.066s
>
> ======================
>
> 增加多线程的版本:
>
> 编译:  $ ghc-6.6.1 -threaded -O fib.hs -o fib
> 执行:  $ time ./fib +RTS -N2
>
> real    0m3.665s
> user    0m5.176s
> sys     0m0.067s
>
> 举这个例子只为试验双核的性能优化
>
>
>
> On 30/11/2007, doudou doudou <array.doudou在gmail.com> wrote:
> > 原来的代码经pyrex编译成C代码之后,再编译成Python扩展模块
> > Python2.4.4
> > Pentium4 3.2GHz
> > Debian etch 4.0
> >
> > 原时间59.130秒
> > 现在41.167秒
> >
> > 已经基本等同于C的代码速度了。
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 18:42

swordsp sparas2006在gmail.com
星期五 十一月 30 18:42:00 HKT 2007

On 11/30/07, vicalloy <zbirder at gmail.com> wrote:
> jython都没什么动静了。JRuby却已是官方的了,JRuby版本的ROR的版本都已突破了1.0。
最近一年又活跃起来了,今年才刚发布了2.2版呢。
不过的确项目状况不太好,代码历史太久了,一些先进的技术、思想都没用到。
整理结构也有点乱,一直没有好好清理过,导致现在维护成本很高。

虽说不要重复发明轮子,但其实我觉得现在如果有一个像xruby这样一个重头开始的新项目出现,可能走得还会轻快些,更容易汇聚社区力量。

> 总体感觉python和.net比较亲。
其实只是因为MS招安了IronPython(和其作者)而已……

> 脚本语言都有自己的局限性,性能一直都不是摆在首要位置的(能快些当然最好了)。
当年Java也是以"脚本语言"身份出现的呢……
现在速度和C++比都已经不太吃亏了。
脚本语言并不是天生就只能做脚本,只是由于有着种种缺陷才被冠以"脚本语言"的名称,以相对突出其强项。
如果这些缺陷都慢慢补上了,那就不是脚本语言,完全有资格成为"新一代的系统语言"了。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 18:47

swordsp sparas2006在gmail.com
星期五 十一月 30 18:47:55 HKT 2007

On 11/30/07, doudou doudou <array.doudou at gmail.com> wrote:
> 晕,相同的算法,C的时间为0.576s。

你确定真的是"相同的算法"么?
我是说,加上了(python内建的)高精度处理么?

很多评测里python这样的脚本语言和c比起来都是吃亏在这种地方,其实是不公平的。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]
zy

zy

2007年11月30日 星期五 20:03

Lich_Ray solo.lich在gmail.com
星期五 十一月 30 20:03:07 HKT 2007

ºðºðºð...ÏàͬµÄËã·¨£¬HaskellÈ«ÃæÊ¤³ö...²»¹ý£¬ÏÖÔÚÓÐÄܹ»²¿·Öʶ±ð˫·µÝ¹éµÄ±àÒëÆ÷£¬¡¶ÏÖ´ú±àÒëÔ­Àí¡·ÊéºóµÄÂÛÎÄÀïÓС£
Ruby²»¾ÍÊÇÏÖÔÚ¿ìÁËÒ»µãÂPython3ÓÖÒªÔÙÂýÒ»µãÂÕâЩһµã¶¼ÊÇÎÞËùνµÄÀ²£¬ÓÐË­¸Ò¸úPerl±ÈËÙ¶È...ÔÙ²»ÐУ¬Lua¶¯Ì¬±àÒë±¾µØÖ´ÐÐÂë¡£

ÔÚ07-11-30£¬swordsp <sparas2006在gmail.com> дµÀ£º
>
> On 11/30/07, doudou doudou <array.doudou在gmail.com> wrote:
> > ÔΣ¬ÏàͬµÄËã·¨£¬CµÄʱ¼äΪ0.576s¡£
>
> ÄãÈ·¶¨ÕæµÄÊÇ"ÏàͬµÄËã·¨"ô£¿
> ÎÒÊÇ˵£¬¼ÓÉÏÁË£¨pythonÄÚ½¨µÄ£©¸ß¾«¶È´¦Àíô£¿
>
> ºÜ¶àÆÀ²âÀïpythonÕâÑùµÄ½Å±¾ÓïÑÔºÍc±ÈÆðÀ´¶¼ÊdzԿ÷ÔÚÕâÖֵط½£¬ÆäʵÊDz»¹«Æ½µÄ¡£
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
Ray Stinger, nickname Lich_Ray
God is in his heaven, all's right with the world.
-------------------------------------------------
let focus = 'computing' in where:
http://lichray.javaeye.com
let focus = 'computing' in here:
http://lichray.bokeland.com
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071130/7361687b/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 21:39

Chuan Qin anewrer在gmail.com
星期五 十一月 30 21:39:36 HKT 2007

但是doudou doudou计算的应该是fib(36) =
14930352,32位机下不会超过int的,如果python连处理这么大的数都要用高精度的话,那也太不常理了。
我猜python花的时间主要都在函数调用上了吧。
fib(36)要好几十秒……

在07-11-30,swordsp <sparas2006在gmail.com> 写道:
>
> On 11/30/07, doudou doudou <array.doudou在gmail.com> wrote:
> > 晕,相同的算法,C的时间为0.576s。
>
> 你确定真的是"相同的算法"么?
> 我是说,加上了(python内建的)高精度处理么?
>
> 很多评测里python这样的脚本语言和c比起来都是吃亏在这种地方,其实是不公平的。
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20071130/35669d66/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 21:44

沈崴 wileishn在gmail.com
星期五 十一月 30 21:44:03 HKT 2007

YARV vs Psyco ...

在 07-11-30,swordsp<sparas2006在gmail.com> 写道:
> On 11/30/07, doudou doudou <array.doudou在gmail.com> wrote:
> > 晕,相同的算法,C的时间为0.576s。
>
> 你确定真的是"相同的算法"么?
> 我是说,加上了(python内建的)高精度处理么?
>
> 很多评测里python这样的脚本语言和c比起来都是吃亏在这种地方,其实是不公平的。
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 21:59

vicalloy zbirder在gmail.com
星期五 十一月 30 21:59:28 HKT 2007

这个测试简单,在我机器上测了下。
JAVA最快,比native的Delphi还快些。
jdk1.6              52594/100=      0.52594s
delphi7           1m05s/100=     0.65s
VC6                 1m11s/100=     0.71s
C#(.net2.0)    14s121/10=       1.4121s
python2.5                                    52s
ruby1.86                                      2m24s
等级顺序是
java>
delphi>vc>>>
C#>>>
python>>
ruby

测试环境:
CPU:Turion64x2 1.6G
内存:2G
虽然CPU是双核的,但似乎这些程序都只用到了一个核心。

备注:
由于JAVA和Delphi的执行时间太短,因此循环执行了100遍,C#执行10遍。
JAVA实在是太快了,在有足够内存,且不考虑垃圾回收的情况下,绝不比C慢。
C#和JAVA太象了,JAVA的代码直接复制过去就OK,都不用改(不过不支持final修饰符)。



===============java===============
	public static final int fib(int n) {
		if (n == 0 || n == 1)
			return n;
		else
			return fib(n - 1) + fib(n - 2);
	}

	public static final void t() {
		for (int i = 0; i < 36; i++) {
			fib(i);
			//System.out.println("n=" + i + "=>" + fib(i));
		}
	}
===============delphi===============
function fib(n: Integer): Integer;
begin
  if (n = 0) or (n = 1) then
    Result := n
  else
    Result := fib(n - 1) + fib(n - 2);
end;

procedure t();
var
  i: Integer;
begin
  for i := 0 to 36-1 do
  begin
    fib(i);
  end;
end;
===============C#===============
	public static int fib(int n) {
		if (n == 0 || n == 1)
			return n;
		else
			return fib(n - 1) + fib(n - 2);
	}

	public static void t() {
		for (int i = 0; i < 36; i++) {
			fib(i);
			//System.out.println("n=" + i + "=>" + fib(i));
		}
	}
===============VC6===============
	fib(int n) {
		if (n == 0 || n == 1)
			return n;
		else
			return fib(n - 1) + fib(n - 2);
	}

	void t() {
		for (int i = 0; i < 36; i++) {
			fib(i);
		}
	}

在 07-11-30,Lich_Ray<solo.lich在gmail.com> 写道:
> 吼吼吼...相同的算法,Haskell全面胜出...不过,现在有能够部分识别双路递归的编译器,《现代编译原理》书后的论文里有。
> Ruby不就是现在快了一点嘛,Python3又要再慢一点嘛,这些一点都是无所谓的啦,有谁敢跟Perl比速度...再不行,Lua动态编译本地执行码。
>
> 在07-11-30,swordsp <sparas2006在gmail.com> 写道:
> > On 11/30/07, doudou doudou < array.doudou在gmail.com> wrote:
> > > 晕,相同的算法,C的时间为0.576s。
> >
> > 你确定真的是"相同的算法"么?
> > 我是说,加上了(python内建的)高精度处理么?
> >
> > 很多评测里python这样的脚本语言和c比起来都是吃亏在这种地方,其实是不公平的。
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
>
> --
> Ray Stinger, nickname Lich_Ray
> God is in his heaven, all's right with the world.
> -------------------------------------------------
> let focus = 'computing' in where:
> http://lichray.javaeye.com
> let focus = 'computing' in here:
> http://lichray.bokeland.com
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>


-- 
Blog http://vicalloy.spaces.live.com/
My googlepage http://vicalloy.googlepages.com/
OldPhoto http://www.lzpian.com/

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]
cyt

cyt

2007年11月30日 星期五 22:34

yuting cui yutingcui在gmail.com
星期五 十一月 30 22:34:05 HKT 2007

所以说和有jit的比要做算法优化之后再说,像fibnacci这种东东jit很容易在调用的时候通过缓存fib(35) fib(34)
fib(32)...的结果来降低复杂度,代价就是不必要的内存开销...当然,也许是通过部分展开之类的(声明成inline之后的vs2005就是展开成fib(35)+fib(34))...或者直接识别出来双路递归?(double
recursion识别的开销低到可以扔在jit里?)
在 07-11-30,vicalloy<zbirder在gmail.com> 写道:
> 这个测试简单,在我机器上测了下。
> JAVA最快,比native的Delphi还快些。
> jdk1.6              52594/100=      0.52594s
> delphi7           1m05s/100=     0.65s
> VC6                 1m11s/100=     0.71s
> C#(.net2.0)    14s121/10=       1.4121s
> python2.5                                    52s
> ruby1.86                                      2m24s
> 等级顺序是
> java>
> delphi>vc>>>
> C#>>>
> python>>
> ruby
>
> 测试环境:
> CPU:Turion64x2 1.6G
> 内存:2G
> 虽然CPU是双核的,但似乎这些程序都只用到了一个核心。
>
> 备注:
> 由于JAVA和Delphi的执行时间太短,因此循环执行了100遍,C#执行10遍。
> JAVA实在是太快了,在有足够内存,且不考虑垃圾回收的情况下,绝不比C慢。
> C#和JAVA太象了,JAVA的代码直接复制过去就OK,都不用改(不过不支持final修饰符)。
>
>
>
> ===============java===============
>         public static final int fib(int n) {
>                 if (n == 0 || n == 1)
>                         return n;
>                 else
>                         return fib(n - 1) + fib(n - 2);
>         }
>
>         public static final void t() {
>                 for (int i = 0; i < 36; i++) {
>                         fib(i);
>                         //System.out.println("n=" + i + "=>" + fib(i));
>                 }
>         }
> ===============delphi===============
> function fib(n: Integer): Integer;
> begin
>   if (n = 0) or (n = 1) then
>     Result := n
>   else
>     Result := fib(n - 1) + fib(n - 2);
> end;
>
> procedure t();
> var
>   i: Integer;
> begin
>   for i := 0 to 36-1 do
>   begin
>     fib(i);
>   end;
> end;
> ===============C#===============
>         public static int fib(int n) {
>                 if (n == 0 || n == 1)
>                         return n;
>                 else
>                         return fib(n - 1) + fib(n - 2);
>         }
>
>         public static void t() {
>                 for (int i = 0; i < 36; i++) {
>                         fib(i);
>                         //System.out.println("n=" + i + "=>" + fib(i));
>                 }
>         }
> ===============VC6===============
>         fib(int n) {
>                 if (n == 0 || n == 1)
>                         return n;
>                 else
>                         return fib(n - 1) + fib(n - 2);
>         }
>
>         void t() {
>                 for (int i = 0; i < 36; i++) {
>                         fib(i);
>                 }
>         }
>
> 在 07-11-30,Lich_Ray<solo.lich在gmail.com> 写道:
> > 吼吼吼...相同的算法,Haskell全面胜出...不过,现在有能够部分识别双路递归的编译器,《现代编译原理》书后的论文里有。
> > Ruby不就是现在快了一点嘛,Python3又要再慢一点嘛,这些一点都是无所谓的啦,有谁敢跟Perl比速度...再不行,Lua动态编译本地执行码。
> >
> > 在07-11-30,swordsp <sparas2006在gmail.com> 写道:
> > > On 11/30/07, doudou doudou < array.doudou在gmail.com> wrote:
> > > > 晕,相同的算法,C的时间为0.576s。
> > >
> > > 你确定真的是"相同的算法"么?
> > > 我是说,加上了(python内建的)高精度处理么?
> > >
> > > 很多评测里python这样的脚本语言和c比起来都是吃亏在这种地方,其实是不公平的。
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese在lists.python.cn
> > > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
> > --
> > Ray Stinger, nickname Lich_Ray
> > God is in his heaven, all's right with the world.
> > -------------------------------------------------
> > let focus = 'computing' in where:
> > http://lichray.javaeye.com
> > let focus = 'computing' in here:
> > http://lichray.bokeland.com
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
>
>
> --
> Blog http://vicalloy.spaces.live.com/
> My googlepage http://vicalloy.googlepages.com/
> OldPhoto http://www.lzpian.com/
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 22:43

vicalloy zbirder在gmail.com
星期五 十一月 30 22:43:27 HKT 2007

又用JS测了下,
在FF下跑了130s,比ruby还快点:)。
opera,safari也差不多。
JS似乎可以利用双核的优势,但CPU并没有跑满。
本想在IE下也做个测试,不过IE老提示我脚本太慢,给暂停了,也就算了。
-- 
Blog http://vicalloy.spaces.live.com/
My googlepage http://vicalloy.googlepages.com/
OldPhoto http://www.lzpian.com/

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月30日 星期五 22:48

东子/hydon hydonlee在gmail.com
星期五 十一月 30 22:48:26 HKT 2007

IronPython°æ::

def fib(n):
   if n == 0 or n == 1:
      return n
   else:
      return fib(n-1) + fib(n-2)

from System import *

dt1 = DateTime.Now
for i in range(36):
    print "n=%d => %d" % (i, fib(i))
print DateTime.Now - dt1

ʱ¼ä: 00:00:19.8906250

XP sp2, .net 2.0, IronPython 2.0A6,
DELL 1501n:: AMD Sempron 3500+, 1G memory

ÔÚ07-11-30£¬ÉòáË <wileishn在gmail.com> дµÀ£º
>
> YARV vs Psyco ...
>
> ÔÚ 07-11-30£¬swordsp<sparas2006在gmail.com> дµÀ£º
> > On 11/30/07, doudou doudou <array.doudou在gmail.com> wrote:
> > > ÔΣ¬ÏàͬµÄËã·¨£¬CµÄʱ¼äΪ0.576s¡£
> >
> > ÄãÈ·¶¨ÕæµÄÊÇ"ÏàͬµÄËã·¨"ô£¿
> > ÎÒÊÇ˵£¬¼ÓÉÏÁË£¨pythonÄÚ½¨µÄ£©¸ß¾«¶È´¦Àíô£¿
> >
> > ºÜ¶àÆÀ²âÀïpythonÕâÑùµÄ½Å±¾ÓïÑÔºÍc±ÈÆðÀ´¶¼ÊdzԿ÷ÔÚÕâÖֵط½£¬ÆäʵÊDz»¹«Æ½µÄ¡£
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
¶«×Ó!
ŬÁ¦×öºÃÿһ¼þÊÂ!!!
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071130/bfed09e9/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号