[BangPypers] python speed comparison

Anand Balachandran Pillai abpillai at gmail.com
Sun Aug 8 13:40:44 CEST 2010


On Fri, Aug 6, 2010 at 8:56 PM, Anand Chitipothu <anandology at gmail.com>wrote:

> Readability counts. Here is my attempt.
>
> def memoize(f):
>    cache = {}
>    def g(a):
>        if a not in cache:
>            cache[a] = f(a)
>        return cache[a]
>    return g
>
> @memoize
> def solve(n):
>    if n == 1:
>        return 1
>    elif n%2 == 0:
>        return 1 + solve(n/2)
>    else:
>        return 1 + solve(3*n+1)
>
> print max((solve(i), i) for i in range(1, 1+1000000))
>

You rock - that was exactly what I was talking about though
I was too lazy to code it :)


>
> $ time python p14.py
> (525, 837799)
>
> real    0m3.981s
> user    0m3.728s
> sys     0m0.242s
>
> Anand
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
--Anand


More information about the BangPypers mailing list