On Sun, Nov 21, 2010 at 7:13 PM, Eviatar <eviatarb...@gmail.com> wrote:
> Ah, the culprit was the preparser. Thank you; disregard my previous
> comments.

No problem.  I bet somebody else will find this message in the
archives later or via a search, and it will help them out.
Also, your example is a good handy example to illustrate the
potentially hugely negative speed consequences of the
preparser on numerical code.  So thanks!

William

>
> On Nov 21, 6:50 pm, William Stein <wst...@gmail.com> wrote:
>> On Sun, Nov 21, 2010 at 4:53 PM, Eviatar <eviatarb...@gmail.com> wrote:
>> > It is actually slower, not just feels like it. Here is a specific
>> > example:
>>
>> > On Sage Notebook (locally):
>>
>> > "from numpy import zeros
>> > from random import randint
>>
>> > def cellular(rule, N, initial='Single-cell'):
>> >    '''Yields a matrix showing the evolution of a Wolfram's cellular
>> > automaton
>>
>> >    rule:     determines how a cell's value is updated, depending on
>> > its neighbors
>> >    N:        number of iterations
>> >    initial:  starting condition; can be either single-cell or a
>> > random binary row
>> >    '''
>> >    M=zeros( (N,2*N+2), dtype=int)
>> >    if initial=='Single-cell':
>> >        M[0,N]=1
>> >    else:
>> >        M[0]=[randint(0,1) for a in range(0,2*N+2)]
>>
>> >    for j in range(1,N):
>> >        for k in range(0,2*N):
>> >            l = 4*M[j-1,k-1] + 2*M[j-1,k] + M[j-1,k+1]
>> >            M[j,k]=rule[ l ]
>> >    return M[:,:-1]
>>
>> > def num2rule(number):
>> >    if not 0 <= number <= 255:
>> >        raise Exception('Invalid rule number')
>> >    binary_digits = number.digits(base=2)
>> >    return binary_digits + [0]*(8-len(binary_digits))
>>
>> > import time
>>
>> > initial=time.clock()
>> > m=cellular(num2rule(90), 100)
>> > print time.clock()-initial"
>>
>> > In Notebook, this returns 5.06. In command-line the same code produces
>> > an output of 0.11 seconds.
>>
>> This has absolutely nothing to do with the notebook.   You can't have
>> this happen if you are actually running the same code.  I just tried
>> this and I get about 5 seconds both in the notebook and on the command
>> line.   *Precisely* how are you running the code on the command line?
>> You can't just paste it in, since it isn't formatted for that.  So
>> maybe you are saving it to a .py file, then using load or import? If
>> you do that, then you'll get much, much different times, because of
>> the Sage preparser, which is not being used on .py files.
>>
>> Try the following in the notebook:
>>
>> (1) Make a cell with exactly this content, and evaluate it:
>>
>> %python
>> from numpy import zeros
>> from random import randint
>>
>> def cellular(rule, N, initial='Single-cell'):
>>    '''Yields a matrix showing the evolution of a Wolfram's cellular
>> automaton
>>
>>    rule:     determines how a cell's value is updated, depending on
>> its neighbors
>>    N:        number of iterations
>>    initial:  starting condition; can be either single-cell or a
>> random binary row
>>    '''
>>    M=zeros( (N,2*N+2), dtype=int)
>>    if initial=='Single-cell':
>>        M[0,N]=1
>>    else:
>>        M[0]=[randint(0,1) for a in range(0,2*N+2)]
>>
>>    for j in range(1,N):
>>        for k in range(0,2*N):
>>            l = 4*M[j-1,k-1] + 2*M[j-1,k] + M[j-1,k+1]
>>            M[j,k]=rule[ l ]
>>    return M[:,:-1]
>>
>> def num2rule(number):
>>    if not 0 <= number <= 255:
>>        raise Exception('Invalid rule number')
>>    binary_digits = number.digits(base=2)
>>    return binary_digits + [0]*(8-len(binary_digits))
>>
>> (2) Make another cell with this content and evaluate it:
>>
>> import time
>> initial=time.clock()
>> m=cellular(num2rule(90), 100)
>> print time.clock()-initial
>>
>> You'll get a fast timing.
>>
>> It would be good for you to learn about the Sage preparser more, since
>> in some cases turning it off (or at least explicitly not using it) can
>> lead to better performance, especially for numerical code.  Turning it
>> off can also lead to much slower (or wrong) code, but usually in the
>> context of symbolic (not numerical) computation.
>>
>> William
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> > On Nov 21, 2:27 pm, William Stein <wst...@gmail.com> wrote:
>> >> On Sun, Nov 21, 2010 at 2:25 PM, William Stein <wst...@gmail.com> wrote:
>> >> > On Sun, Nov 21, 2010 at 1:33 PM, Eviatar <eviatarb...@gmail.com> wrote:
>> >> >> I don't know if this is related, but I've noticed even the local Sage
>> >> >> Notebook is slower than command-line, specifically NumPy operations.
>>
>> >> > It makes absolutely no sense that that could happen.    Please give a
>> >> > specific example.
>>
>> >> More precisely, please define exactly what you mean by "slower".  Do you 
>> >> mean:
>>
>> >>   (1) it "feels" slower,
>> >>   (2) the time from press shift-enter until you see output is longer, or
>> >>   (3) When you use "time a_specific_command(...)", it takes longer.
>>
>> >> What I don't think could happen is (3).   Obviously (1) or (2) could
>> >> happen, due to the client server architecture.
>> >> However, this would have absolutely nothing to do with numpy, and you
>> >> would notice the same slowdown with
>> >> any code in Sage.
>>
>> >> William
>>
>> >> >  -- William
>>
>> >> >> On Nov 21, 12:30 pm, William Stein <wst...@gmail.com> wrote:
>> >> >>> On Sun, Nov 21, 2010 at 5:23 AM, tuxiano <tuxi...@gmail.com> wrote:
>> >> >>> > Please, don't disablewww.sagenb.org
>> >> >>> > I'm not an administrator of my office PC so I can't install SAGE and
>> >> >>> > my company filters connections not on port 80 or 8080 so  
>> >> >>> > www.sagenb.org
>> >> >>> > is the only Sage implementation that I can use.
>>
>> >> >>> There is alsohttp://demo.sagenb.organdhttp//demo2.sagenb.org.
>>
>> >> >>> William
>>
>> >> >>> > Perhaps other people
>> >> >>> > are in my condition.
>>
>> >> >>> > Thanks
>>
>> >> >>> > Tiziano
>>
>> >> >>> > On Nov 21, 3:18 am, Jeroen Demeyer <jdeme...@cage.ugent.be> wrote:
>> >> >>> >> On 2010-11-20 22:45, William Stein wrote:
>>
>> >> >>> >> > (1) I simply totally disablewww.sagenb.org,
>>
>> >> >>> >> Maybe keepwww.sagenb.orgasitisbut remove the link from the Sage
>> >> >>> >> front page?
>>
>> >> >>> > --
>> >> >>> > 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 
>> >> >>> > athttp://groups.google.com/group/sage-devel
>> >> >>> > URL:http://www.sagemath.org
>>
>> >> >>> --
>> >> >>> William Stein
>> >> >>> Professor of Mathematics
>> >> >>> University of Washingtonhttp://wstein.org
>>
>> >> >> --
>> >> >> 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 
>> >> >> athttp://groups.google.com/group/sage-devel
>> >> >> URL:http://www.sagemath.org
>>
>> >> > --
>> >> > William Stein
>> >> > Professor of Mathematics
>> >> > University of Washington
>> >> >http://wstein.org
>>
>> >> --
>> >> William Stein
>> >> Professor of Mathematics
>> >> University of Washingtonhttp://wstein.org
>>
>> > --
>> > 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 
>> > athttp://groups.google.com/group/sage-devel
>> > URL:http://www.sagemath.org
>>
>> --
>> William Stein
>> Professor of Mathematics
>> University of Washingtonhttp://wstein.org
>
> --
> 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
>



-- 
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

-- 
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