On Tue, Oct 21, 2008 at 4:09 PM, Dan Drake <[EMAIL PROTECTED]> wrote:
> On Tue, 21 Oct 2008 at 07:50AM -0700, pong wrote:
>> I wrote two list tests:
>>
>> def test1():
>>       for k in range(10^3):
>>             v=[2*random() for j in range(10)]
>>       else: pass
>>
>> def test2():
>>        for k in range(10^3):
>>              v=list(2*vector([random() for j in range(10)]))
>>        else: pass
>>
>> It turns out that
>> time test1() gives around 8s CUP time while time test2() shows only
>> 0.35s CUP time is used. Wow!
>
> Also, instead of doing your own loops, you can use %timeit:
>
> sage: %timeit [2*random() for j in range(10)]
> 100000 loops, best of 3: 6.87 µs per loop
> sage: %timeit list(2*vector([random() for j in range(10)]))
> 10000 loops, best of 3: 112 µs per loop
>
> Using list comprehensions is almost always a good choice in Sage/Python;
> as I understand (and as we see above), they're very well-optimized.

I also added a command called timeit, so you can do

sage: timeit(' [2*random() for j in range(10)]')

this has the advantage that the input is preparsed using
the Sage preparser, so might give a slightly more accurate
result, and work in some cases where %timeit doesn't work.
For example,

sage: %timeit R.<x> = QQ[]
------------------------------------------------------------
   File "<magic-timeit>", line 6
     R.<x> = QQ[]
       ^
SyntaxError: invalid syntax

sage: timeit('R.<x> = QQ[]')
625 loops, best of 3: 287 µs per loop

It would be reasonable to argue that the fact that %timeit doesn't
preparse its input is a bug in Sage (actually IPython), and that it
should.  I've cc'd Fernando Perez (author of IPython) in case he has a
comment.

William

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to