On 23 Sep., 10:19, javier <vengor...@gmail.com> wrote:
> On 23 sep, 06:57, "D. S. McNeil" <dsm...@gmail.com> wrote:
>
> > I think replacing 2/3 with 2./3 in
>
> >                 if abs(varia-root)<2/3:
>
> > will help a little.. :^)  By itself, that cuts the time down to 19s for me.
>
> Slightly better, save yourself doing the same computation over and
> over inside all the loops
>
>     if abs(varia-root) < 0.67
>
> it is a very little time but adds up quickly over the iterations. Or
> if you want to be finer, define the 2./3 as a constant outside of the
> loops (with whichever precision you need) and then use the defined
> constant inside.

Any reasonable compiler does constant folding and loop-invariant code
motion; even Python's byte-code compiler should do that.

A smarter compiler would [have] remove[d] *the whole loop*, since the
loop body is completely dead code (for 2/3 rather than 2.0/3, because
abs(...)<0 will never hold).

The latter is perhaps inhibited or complicated by Python because the
last value of 'root' from the first loop might actually get used later
-- *after* the second loop iterating over 'roots', i.e., because e.g.

    for root in []: ...

does *not* change the (previous) value of 'root'. (In fact, the
construct above doesn't define 'root' at all.)

In the given code, this shouldn't be a problem though, since 'root'
isn't referenced after the second loop (assuming it is a *local*
variable).


-leif

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to