On Nov 15, 2008, at 23:57 , pong wrote:
> I was a bit reluctant to post this question here since "support"
> shouldn't mean teaching me how to write programs. But Jason Grout
> suggested me to post it here anyway.
It seems reasonable to post this here, since it involves performance,
which can be tricky in a complicated system like Sage. I don't think
this qualifies as "teaching you to program" :-}
> Here is a very short script that I want to maximize the speed
>
> def lspec(n):
> return sorted([k if k^2 < 2*n else 2*n/k for k in divisors
> (odd_part(n))])
>
> At first, I think I can make it faster by avoiding computing k^2 in
> the for loop by replacing k^2 > 2*n by
> k > r where r = sqrt(RDF(2*n)) is computed once outside the for loop.
> However, it looks like comparing two entities of different types is
> very slow in SAGE so I ended up with a slower script if a do that.
I assume you mean "<" instead of ">" (or vice versa in your script).
Try using isqrt() to compute the integer part of the square root. It
speeds up the loop, but only slightly:
sage: timeit(" lspec(2799734567890)")
625 loops, best of 3: 1.24 ms per loop
sage: timeit(" sspec(2799734567890)")
625 loops, best of 3: 1.13 ms per loop
(This is on a 3GHz Dual Quad Xeon, so YMMV). sspec() is the version
with the square root computed outside the loop.
HTH
Justin
--
Justin C. Walker, Curmudgeon-At-Large
Institute for the Enhancement of the Director's Income
--------
Experience is what you get
when you don't get what you want.
--------
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---