Re: Timing of looping operators
- To: mathgroup at smc.vnet.net
- Subject: [mg62420] Re: [mg62416] Timing of looping operators
- From: "Hermann Schmitt" <schmitther at t-online.de>
- Date: Wed, 23 Nov 2005 06:27:31 -0500 (EST)
- References: <200511230931.EAA20248@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hello Daniel,
loops are normally more efficient then recursive function calls, because in
function calls more temporary variables have to be created.
In my oo system I first tried recursive function calls, but then changed to
loops.
Hermann Schmitt
----- Original Message -----
From: "dh" <dh at metrohm.ch>
To: mathgroup at smc.vnet.net
Subject: [mg62420] [mg62416] Timing of looping operators
> 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
>
- References:
- Timing of looping operators
- From: dh <dh@metrohm.ch>
- Timing of looping operators