Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Timing of looping operators

  • To: mathgroup at smc.vnet.net
  • Subject: [mg62425] Re: [mg62416] Timing of looping operators
  • From: Pratik Desai <pdesai1 at umbc.edu>
  • Date: Thu, 24 Nov 2005 06:33:23 -0500 (EST)
  • References: <200511230931.EAA20248@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

dh wrote:

>Hello,
>The Mathematica gospel tells you that you should NOT use loops because 
>it is inefficient.
>Well consider the following examples and their times:
>
>n=10^6;
>d=Table[i,{i,n}];
>fun[x_]:=x;
>
>a)  Timing[fun & /@ d;]       needs 0.8 sec
>b)  Timing[Scan[fun, d]]      needs 1 second
>c)  Timing[Do[f[i], {i, n}];] needs 0.7 sec
>
>a) applies the function and creates a new list. b) does not create a new 
>list -- but it is slower! And finally c) the loop is fastest!!!
>
>If you change the function to: f[x_]:=x^2, the times are even more striking:
>
>0.8, 2.4, 0.7
>it seems like in a and c the function evaluation takes negliable time 
>compared to the loop construct, but not so in b.
>
>has anybody an explanation???
>
>Daniel
>
>  
>
Looking at your expression, one can just use table alone

d=Timing[Table[i,{i,n}];]

>>{0.06 Second, Null}

In mathematica, IMHO, table is the primary loop construct

Hope this helps

Pratik .

-- 
Pratik Desai
Graduate Student
UMBC
Department of Mechanical Engineering
Phone: 410 455 8134



  • Prev by Date: Re: permutations
  • Next by Date: Re: Modeling Saturation - Sensor or Acutuator
  • Previous by thread: Re: Timing of looping operators
  • Next by thread: Re: Timing of looping operators