Date: Feb 23, 2013 11:31 PM
Author: Ray Koopman
Subject: Re: Compiling numerical iterations
On Feb 23, 4:01 am, firlefranz <cornelius.fr...@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[1234567890]

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[1234567890]

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.