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

T

thattommyhallll

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top