Re: Nested iterators in Compile
- To: mathgroup at smc.vnet.net
- Subject: [mg55045] Re: Nested iterators in Compile
- From: Jon Harrop <usenet at jdh30.plus.com>
- Date: Thu, 10 Mar 2005 05:25:06 -0500 (EST)
- References: <d0mo9g$75h$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Maxim wrote: > The fact that iterators work differently when used in Compile seems to > lead to a serious limitation: Yes, this may be a bug. > The iterator {j, i} is evaluated before any value is assigned to i, and > the evaluation of the compiled code fails (and if we add the > initialization i=0 before Do, we'll only get an empty loop as the result, > unlike the uncompiled version). So we have to resort to some workarounds > such as changing the inner iterator to {j, n} and adding If[j>i, > Continue[]] to the loop body, which of course decreases the performance. You can nest your Do loops, giving a 21x performance improvement in this case: In[1] := Function[{n}, Module[{s = 0}, Do[s += j, {i, n}, {j, i}]; s] [1000] // Timing Out[1] = {2.33 Second, 167167000} In[2] := Compile[{n}, Module[{s = 0}, Do[Do[s += j, {j, i}], {i, n}]; s]] [1000] // Timing Out[2] = {0.11 Second, 167167000} -- Dr Jon D Harrop, Flying Frog Consultancy http://www.ffconsultancy.com