can anybody explain this range() speedup (with psyco)

thattommyhallll@gmail.com thattommyhall at gmail.com
Thu Jul 27 20:10:57 EDT 2006


i am doing the problems at
http://www.mathschallenge.net/index.php?section=project
one problem involved finding factors, i used

def divisors(n):
    divisors = set([1])
    for i in range(1, math.ceil(n ** 0.5)+1):
        if n % i == 0:
            divisors.add(i)
            divisors.add(n/i)
    return divisors

and it took 16s to solve the task, not too bad.
using psyco brought this down to 5s, nice
i tried it in ironpython and got an error in the range() function,
which i corrected by changing it to
for i in range(1, int(n ** 0.5)+1):
and ironpython took 9s.

when i reran this in psyco, i got a 2x speed improvement (down to 2s)

ive just noticed after leaving idle that there is a depreciation
warning about non-int args to range(), but my question is, why can me
bunging a int( ) in there be faster than whatever python does to
non-int args?

where can i get info on performance of algorithms, ideally with ref to
python, etc?
ive got Python Programming:An intro to computer science, but it seems a
bit basic. anybody know of good unis/colleges using python on their com
sci courses?

its nice to look at the forums on the mathschallenge page and see how
python is the sweet spot between concise/readable. ive been tempted by
ruby but it does not scan nearly as well.




More information about the Python-list mailing list