Thanks to both Dan and William. However, Dan's result puzzled me. Aren't they suggested that the for loop is faster?
Here is what I got: sage: timeit('list(2*vector([random() for j in range(10)]))') 625 loops, best of 3: 332 µs per loop sage: timeit('[2*random() for j in range(10)]') 125 loops, best of 3: 8.3 ms per loop which suggested otherwise. Also how does timeit determine how many loops to perform in a test? On Oct 21, 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. > > Dan > > -- > --- Dan Drake <[EMAIL PROTECTED]> > ----- KAIST Department of Mathematical Sciences > ------- http://mathsci.kaist.ac.kr/~drake > > signature.asc > < 1KViewDownload --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---