Python in game development? + recFib

Christian Tanzer tanzer at swing.co.at
Thu Aug 31 02:10:21 EDT 2000


"Darrell Gallion" <darrell at dorb.com> wrote:

> Hacked up Christian's code to get past the stack limit.
> Skipped locking on things like the cache since it's a write once kind of
> thing.
> Guess hacking can pay off.

(snip -- 90 lines of code)

There's an easier way:

def fibonacci (n, cache = [0L, 1L]) :
    for i in range (len (cache), n+1) :
        cache.append (cache [i-1] + cache [i-2])
    return cache [n]
# end def fibonacci

And it's more efficient, too (for n=10000, it's 50% faster than
my version with the dict; for n=16383, it's almost 3 times faster
than your version).

Simple-is-often-faster-than-complex 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