Re: Unusual speedup of Table
- To: mathgroup at smc.vnet.net
- Subject: [mg25617] Re: [mg25605] Unusual speedup of Table
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Mon, 16 Oct 2000 03:04:40 -0400 (EDT)
- References: <200010110750.DAA18883@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
SATether wrote: > > Why is the second call of Table below so much faster than the first? These > results are from Mathematica 4.0.2.1 for Power Mac (400 MHz PPC 750, OS 9). It > isn't some kind of cache effect because the times are consistent over many > executions of both expressions. (And yes, I know I can do a lot better still > for this particular computation with Drop and Take). > > - Steve Tether > > n = 10000; > a = Table[Random[], {n}]; > Timing[Table[a[[j + 1]] - 2 a[[j]] + a[[j - 1]], {j, 2, n - 1}];] > > {10.5333 Second, Null} > > Timing[Table[Switch[j, _, a[[j + 1]] - 2 a[[j]] + a[[j - 1]]], {j, 2, n - 1}];] > > {0.7 Second, Null} I do not know why the first Table construction is slow. I suspected some needless unpacking, but this is not the case. Specifically, if I first do Developer`SetSystemOptions["UnpackMessage"->True]; then I get no message when running your Table construction. On a more optimistic note, this slowness has apparently been addressed in the Mathematica kernel that will be in a future release. Below I show this on a list 100 x as large as yours. In[6]:= n = 1000000; In[7]:= a = Table[Random[], {n}]; In[8]:= Timing[t1 = Table[a[[j + 1]] - 2 a[[j]] + a[[j - 1]], {j, 2, n - 1}];] Out[8]= {25.17 Second, Null} This is significantly faster than what I get using the Table[Switch...]] workaround, as it should be. Note that one can significantly improve speed of such a computation using ListCorrelate. In[9]:= Timing[t2 = ListCorrelate[{1,-2,1}, a];] Out[9]= {1.7 Second, Null} In[10]:= Max[Abs[t1-t2]] -16 Out[10]= 4.44089 10 Daniel Lichtblau Wolfram Research
- References:
- Unusual speedup of Table
- From: satether@aol.com (SATether)
- Unusual speedup of Table