MathGroup Archive 2008

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

Search the Archive

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

And I was surprised much more when I have tried the more simple form
of the first expression:

Timing[icount = 0;
 Do[icount += i, {i, 1, 10000000, 1}];
(*this returns
{36.042, 50000005000000}
on my machine*)

It means that using the "+=" operator takes about 50% more time than
the usual form
icount = icount + i;
It is sad and very surprising!

On 24 =C1=D0=D2, 14:06, "W_Craig Carter" <ccar... at> wrote:
> On Wed, Apr 23, 2008 at 3:09 PM, AES <sieg... at> 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.
> > =9AThanks for comments.
> > =9A_Why_ (or, in what circumstances?) would Table be a better choice tha=
n Do?
> > =9AI 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. =9AAnd they (Table
> and Do) are just about interchangeable, except in a few unusual (and
> technical) cases. =9ATable'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 =9Amain kernel's
> evaluation loop, and "Table" doesn't).
> =9AI'll try to demonstrate this as an example. I'll try to explain my
> steps carefully, and I'll avoid the use of #&/.^= syntax. =9AIt 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[
> =9Aicount = 0;
> =9ADo[icount = icount + i, {i, 1, 10000000, 1}];
> =9Aicount
> =9A]
> (*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[
> =9ATotal[Table[i, {i, 1, 10000000, 1}]]
> =9A]
> (*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. =9ATable 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 =9Ain the mind of a beginner who has neither
> seen Do, For, While, or Table. Which style would you recommend based
> on speed alone? =9AWhich style would you recommend on readability alone?
> So, a typical beginning user might find themselves tending to use
> Lists. =9AThey 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...). =9AThey 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 anyw=
> (*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 bracket=
> (* 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. =9ASometimes, 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: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)
  • Next by Date: Re: Re: installing Playe rPro killed using Mathematica itself:
  • Previous by thread: RE: 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*)