Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #1612

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

From Peter Pein <petsie@dordos.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: How to Compile this code (multiple random walks)
Date 2011-04-12 09:54 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <io17g0$i59$1@smc.vnet.net> (permalink)
References <inuncc$2dl$1@smc.vnet.net>

Show all headers | View raw


Am 11.04.2011 13:06, schrieb mfific@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.

Back to comp.soft-sys.math.mathematica | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to  Compile this code (multiple random walks) "mfific@gmail.com" <mfific@gmail.com> - 2011-04-11 11:06 +0000
  Re: How to  Compile this code (multiple random walks) Peter Pein <petsie@dordos.net> - 2011-04-12 09:54 +0000
  Re: How to Compile this code (multiple random walks) Derek Yates <yatesd@me.com> - 2011-04-12 09:54 +0000

csiph-web