MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg88143] RE: [mg88122] Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)
  • From: "Jose Luis Gomez" <jose.luis.gomez at itesm.mx>
  • Date: Sat, 26 Apr 2008 03:43:40 -0400 (EDT)
  • References: <fupm2j$s4a$1@smc.vnet.net> <200804250929.FAA08198@smc.vnet.net>

Dear Alexey

I am Not surprised that a functional-programming and list-oriented language
like Mathematica is much slower if you force it to Not to construct a list.

Mathematica is made for working with lists, and structures similar to lists,
for example evaluate 
FullForm[{a,b,c}] 
After that evaluate 
FullForm[a+b+c]
Then you will see the kind of structures that Mathematica uses internally.

Therefore the natural behavior of Mathematica is to create lists (and
similar structures)

Forcing Mathematica to NOT create a list, can make it SLOWER.

>From the point of view of a functional-programming and list-oriented
language (like Mathematica) the Do[] loop IS NOT THE SIMPLEST, as you called
it.

Perhaps the document in the following links can be interesting for you:
http://homepage.cem.itesm.mx/lgomez/matecmatica/funcionalprog.nb 
http://homepage.cem.itesm.mx/lgomez/matecmatica/fp/fp.html

Hope that helps

Jose
Mexico

-----Mensaje original-----
De: Alexey Popkov [mailto:popkov at gmail.com] 
Enviado el: Viernes, 25 de Abril de 2008 04:30 a.m.
Para: mathgroup at smc.vnet.net
Asunto: [mg88122] Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)

I should notice that it is VERY strange behavour that the simplest
Do[] loop takes MUCH more time than constructing a list of elements
and summing them. I do not understand the reason. It looks like a
defect.

W_Craig Carter:
> On Wed, Apr 23, 2008 at 3:09 PM, AES <siegman at stanford.edu> wrote:
> >
> > > Table is usually a better choice than Do, but that is a choice and the
> > > language is accommodating enough to allow you to make that choice; and
> > > if you should be curious, to explore.
> > >
> >  Thanks for comments.
> >
> >  _Why_ (or, in what circumstances?) would Table be a better choice than
Do?
> >
> >  I appreciate that their logical behavior is similar, and indeed they
can be
> > coded very similarly.
>
> Dear AES,
> I am happy to try to answer.
>
> I'll take the time to answer at length, because I find that many
> people that I try to help out have similar viewpoints as yours.
> Perhaps, I may have something useful to contribute.
>
> Primarily, it is a matter of choice and familiarity.  And they (Table
> and Do) are just about interchangeable, except in a few unusual (and
> technical) cases.  Table's advantage is speed and efficiency. (Once I
> asked someone at Wolfram how I should explain this to my students, and
> the kind answer was because "Do" has to access the  main kernel's
> evaluation loop, and "Table" doesn't).
>
>  I'll try to demonstrate this as an example. I'll try to explain my
> steps carefully, and I'll avoid the use of #&/.^= syntax.  It took me
> a while to cook this up--I hope that my example doesn't go amiss.
>
> This is problem with a known simple result, but it will serve: let's
> find the sum of the first 10000000 integers.
>
> (*let's use a do loop*)
> Timing[
>  icount = 0;
>  Do[icount = icount + i, {i, 1, 10000000, 1}];
>  icount
>  ]
>
> (*this returns {10.2826, 50000005000000} on my machine.*)
> (*10.28 being a measure of how long the computation took to run*)
>
> (*lets try a table*)
> Timing[
>  Total[Table[i, {i, 1, 10000000, 1}]]
>  ]
>
> (*This returns {3.25516, 50000005000000} on my machine*)
>
> This is a simple example, but it illustrates the difference nicely. (
> Personally, I've yet to
> find a case for which I can replace a Do with a Table or similar list
> construct, although I am sure readers of this group could easily
> construct one as a challenge.)
>
>
> Another way to think of this is that Mathematica is very good at
> operating on lists.  Table and Lists work together nicely, and many of
> the functions like Total are designed to help you make your
> programming easier.
>
> Now, if you put yourself  in the mind of a beginner who has neither
> seen Do, For, While, or Table. Which style would you recommend based
> on speed alone?  Which style would you recommend on readability alone?
>
> So, a typical beginning user might find themselves tending to use
> Lists.  They are content for a couple years, but soon master the idea
> so that begin to do what is natural, abbreviate (ASAP, RJ46, LOL,
> AWOL, PM. FM...).  They find shortcuts and optimize their time:
>
> First attempt may be something like this:
> (*create a list*)
>
> Table[i,{i,1,10000000}]
>
> (*horrors, the output is too long, and I know what it might look like
anyway*)
>
> (*they graduate to this*)
> mytable= Table[i,{i,1,1000000}];
> Total[mytable]
>
> (* soon they get tired of typing and find an abbreviated version *)
>
> Total[Table[i,{i,1,1000000}]]
>
> (* soon they get tired of moving around the screen to type the two
brackets,*)
> (* so they find another way to do the same thing, no better, arguably
> less readable
> (*at first*)
> Total@Total[Table[i,{i,1,1000000}]
> (*or, depending on where your cursor tends to be sitting*)
> Table[i, {i, 1, 1000000}] // Total
>
> (*thus they learn a semaphore for analogous constructs*)
> (*but there's more, with ever more symbols, the efficiency and power
goes*)
> (*up rapidly, but the readability goes down for beginners*)
> (*where you find the happy medium is entirely up to you*)
>
>
> In my opinion, this is the fun part of mathematica. I get to decide
> how to share the labor with mathematica, and I get to decide how
> obscure to make my code.  Sometimes, for my own pleasure, I write it
> as obscurely as possible (and I am no master at this by a long shot).
> If I am writing code that I hope someone else will use and modify, or
> learn from, I try to write as readable as possible.
>
>
> --
>
> W. Craig Carter




  • Prev by Date: Re: 2*m^z - m^z = ?
  • Next by Date: Re: Wolfram User Interface Research?
  • Previous by thread: Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)
  • Next by thread: Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)