MathGroup Archive 2011

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

Search the Archive

Re: How to Compile this code (multiple random walks)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg118116] Re: How to Compile this code (multiple random walks)
  • From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
  • Date: Tue, 12 Apr 2011 06:54:06 -0400 (EDT)

On Tue, 12 Apr 2011, Peter Pein wrote:

> Am 11.04.2011 13:06, schrieb mfific at gmail.com:
>> Transpose[{Range[0, Length[#] - 1], #}]&@
>>   NestWhileList[(# + {If[Random[]>  .5, 1, -1],
>>        If[Random[]>  .5, 1, -1], If[Random[]>  .5, 1, -1]}
>>      )&, {0, 0, 0}, -5<  #[[1]]<  5&&  -5<  #[[2]]<  5&&  -5<  #[[3]]
>> <  5&  ]
>
> Hi Mario,
>
> you _can_ compile the part that will be given to Transpose but
> NestWhileList seems to be efficient enough.
>
> I tried your version and
>
> count3helper=Compile[{},
>   NestWhileList[# + 2 RandomInteger[1, {3}] - 1 &,
>     {0, 0, 0},
>     -5 < #[[1]] < 5 && -5 < #[[2]] < 5 && -5 < #[[3]] < 5 &],
>   {{_NestWhileList, _Integer, 2}},
>   CompilationTarget -> "C",
>   Parallelization -> True, RuntimeAttributes -> {Listable}
> ];
>
> count3[] := Transpose[{Range[0, Length[#] - 1], #} &@
>   count3helper[]
> ]
>
> using ParallelTable some times 10^6 times each and got timings for your
> version of ~25 seconds and ~32 seconds for the compiled version above.
>
> Sorry,
> Peter
>
> P.S.: I used
>
> RandomSeed[1];
> {tim, hst} = AbsoluteTiming[Histogram[ParallelTable[Length[count3[]],
>  {10^6}], PlotLabel -> "time here"]];
> hst /. "time here" -> ToString[NumberForm[tim, 4]]
>
> for testing.
>
> P.P.S.:
>
> changing the test in NestWhileList to
> -5 < Min[#] && Max[#] < 5 &
> results in timings ~24.3s for your and 27.6s for the compiled version.
>
>

Hi Peter,

good idea with the _NestWhileList. I forgot about that. However, if you 
look at

Needs["CompiledFunctionTools`"]
CompilePrint[count3helper]

there is still the call to MainEvaluate.

A small note, the options Parallelization -> True, RuntimeAttributes -> 
{Listable} would only make a difference if the function had, say, an

{in, _Integer, 0}

type of input. Then, you could call the function with a integer vector and 
the code would run in parallel.

Hth,

Oliver


  • Prev by Date: Re: Plotting Locators with Color
  • Next by Date: Polygon Data to Graph
  • Previous by thread: Re: How to Compile this code (multiple random walks)
  • Next by thread: MathML, JSP and webMathematica