Python in game development?

Christian Tanzer tanzer at swing.co.at
Wed Aug 30 02:02:36 EDT 2000


kragen at dnaco.net (Kragen Sitaker) wrote:

> 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)

The timing of the optimized version is quite volatile -- I get results
between 0m0.055s and 0m0.167s, while your version consistently uses
more than 50 seconds.

Now that optimization is obvious. My point is that Python favors
optimization much more than some of the other languages you
benchmarked (C the most obvious case, I don't know about Forth).

Don't-trust-benchmarks, ly
Christian

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92





More information about the Python-list mailing list