Re: Table[] Why such a hog?
- To: mathgroup at smc.vnet.net
- Subject: [mg3573] Re: Table[] Why such a hog?
- From: espen.haslund at fys.uio.no (Espen Haslund)
- Date: Mon, 25 Mar 1996 21:35:03 -0500
- Organization: Universitet i Oslo
- Sender: owner-wri-mathgroup at wolfram.com
In article <4iqpo1$puj at dragonfly.wolfram.com>, newnews at almaden.ibm.com says... > >Seems like the Table[] function for building arrays takes way longer than it ought to. I hope someone can >tell me why: > >The following expression takes around twenty minutes to complete on my RS/6000 workstation: > >Table[Exp[N[2 Pi I ((i-32)^2 + (j-32)^2)/64]], {i,.001,63.876,.125},{j,.001,63.876,.125}] > >Why should this take so much resource? Is there a better way to generate an array of function values? >What is Mathematica doing that take so long? > >BTW, the equivalent expression in APL completes on the same machine in less than 6 seconds. > >Bob Shelby > > Hi, Bob, My idea is to use a CompiledFunction. I still had to shorten your example to run it. IN: f[i_, j_] := Exp[N[2 Pi I ((i-32)^2 + (j-32)^2)/64]] IN: g = Compile[{i,j},Evaluate[f[i, j] ] ] OUT: CompiledFunction[{i, j}, 2 2 0.0981748 I ((-32. + i) + (-32. + j) ) E , -CompiledCode-] IN: Timing[fdata = Table[f[i, j], {i,.001,5.,.125},{j,.001,5.,.125}]; ] OUT: {6.59 Second, Null} IN: Timing[gdata = Map[g[#[[1]], #[[2]] ] &, Table[{i,j}, {i,.001,5.,.125},{j,.001,5.,.125}], {2} ];] OUT: {1.54 Second, Null} IN: Chop[gdata - fdata] == 0*fdata OUT: True -Espen ==== [MESSAGE SEPARATOR] ====