Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg88122] Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)
  • From: Alexey Popkov <popkov at gmail.com>
  • Date: Fri, 25 Apr 2008 05:29:47 -0400 (EDT)
  • References: <fupm2j$s4a$1@smc.vnet.net>

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: Mathematica courses and news from the Benelux
  • Next by Date: Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)
  • Previous by thread: Re: Re: Print vs Print[text, Plot]? (*now Do and Table*)
  • Next by thread: RE: Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)