lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


The following is from the Python newsgroup. Interesting how using features
of the language to optimise a routine can be so effective.

Anyway, I was wondering how the C interpreters would come out against this.
eg. EiC,Bob etc. Mr Jeske have you tried any of these?

Nick


> On 2000-04-22, I did a microbenchmark consisting of the stupid way to
> compute Fibonacci numbers; the Python version was as follows:
>
> #!/usr/bin/python
> def recfib(n):
>         if n <= 2:
>                 return 1
>         else:
>                 return recfib(n-1) + recfib(n-2)
>
> print recfib(33)
>
> Here were my results, updated by more data from the next few days:
>
>  These are the 'user' times from bash's 'time' command on my K6-2/450
>  running Linux 2.2.14 with, presumably, glibc 2.1.
>   . . .
>  So here's the original table, with bad entries removed and new entries
>  extrapolated on the basis of minimal evidence:
>
>     C compiled with gcc 2.95.2: 0m0.230s (see note 2)
>          GForth 0.4.9-19990617: 0m2.360s
>             pfe (FORTH) 0.9.14: 0m2.390s
>                     pforth V21: 0m5.340s
>           RScheme v0.7.3.1-b39: 0m8.110s
>                        Lua 3.2: 0m9.280s
>  GhostScript (PostScript) 5.10: 0m9.530s
>               scm (Scheme) 5d2: 0m9.880s
>           guile (Scheme) 1.3.4: 0m30.950s
>           Python 1.5 (I think): 0m31.280s
>                  Perl 5.005_03: 0m44.850s
>                        gprolog: 47s (extrapolated)
>             Klassaized Tcl 8.2: 1m11.621s
>                           hugs: 2m12s (extrapolated)
>                  pdksh v5.2.14: 7m32s (extrapolated)
>                 bash 2.03.0(1): 36m08s (extrapolated)
(snip)
> Still, it is clear that a number of scripting languages are
> considerably faster than Python for some tasks: FORTH, Smalltalk, Lua,
> and Scheme all come out at least three times faster, and are all
> scripting languages by some definition.  Some interpreted FORTH
> implementations are ten times faster.

Quite interesting.

It took me two minutes to optimize your Python code by more than a
factor of 500 (Python 1.5.2 on a PentiumPro 200 running Linux 2.0.29,
libc5):

def recfib (n, cache = {}) :
    if not cache.has_key (n) :
        if n <= 2:
            cache [n] = 1
        else:
            cache [n] = recfib (n-1) + recfib (n-2)
    return cache [n]

print recfib(33)