       Re: Compiling numerical iterations

• To: mathgroup at smc.vnet.net
• Subject: [mg129896] Re: Compiling numerical iterations
• From: Ray Koopman <koopman at sfu.ca>
• Date: Sat, 23 Feb 2013 23:31:20 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• Delivered-to: l-mathgroup@wolfram.com
• Delivered-to: mathgroup-newout@smc.vnet.net
• Delivered-to: mathgroup-newsend@smc.vnet.net
• References: <kgab2i\$c7f\$1@smc.vnet.net>

```On Feb 23, 4:01 am, firlefranz <cornelius.fr... at gmx.net> wrote:
> Hi,
>
> to speed up some calculations I'd like to use this relatively new method of compiling equations or export them to C. But since I'm very new in these things, I have really problems to use this c-compiling function.
> Can someone show me on this simple part of code (calculation of a correlation function of a 1D random walk), how it should be implemented?

I see little need to compile. Here is your code
(with 'l' changed to'j' to avoid confusion with '1'),
with times for the two outer loops:

SeedRandom
num = 10^3
tab = Table[0, {num}];
corr = Table[0, {num/10}];
AbsoluteTiming@For[j = 1, j <= 1000, j++,
a = 0;
For[i = 1, i <= num, i++,
tab[[i]] = tab[[i]] + a/10;
If[RandomReal[{-1,1}] < 0, a = a + 1, a = a - 1]]]
AbsoluteTiming@For[k = 1, k <= num/10, k++,
For[n = 1, n <= num/10*9, n++,
corr[[k]] = corr[[k]] + tab[[n]]*tab[[n + k - 1]]/num*10/9]]
corr1 = corr;

1000
{22.882292, Null}
{2.836433, Null}

Here are the corresponding times for reorgnized code
that gives the same 'corr':

SeedRandom
AbsoluteTiming@Length[ tab = FoldList[Plus,0,
Total@Sign@Table[Most@RandomReal[{-1,1},num],{1000}]] ]
AbsoluteTiming@Length[ corr = Table[Take[tab,{k,num*9/10-1+k}],
{k,num/10}].Drop[tab,-num/10]/(num*10*9) ]
corr == corr1

{0.262409,1000}
{0.007661,100}
True

Most@RandomReal[{-1,1},num] should be RandomReal[{-1,1},num-1],
but I generated 'num' randoms and then dropped the last one
to keep them the same as in your code.

```

• Prev by Date: Re: Mathematica and Lisp
• Next by Date: Hold & Evaluate
• Previous by thread: Re: Compiling numerical iterations
• Next by thread: Re: Compiling numerical iterations