Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Compiling numerical iterations
Replies: 7   Last Post: Mar 5, 2013 4:15 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Ray Koopman

Posts: 3,362
Registered: 12/7/04
Re: Compiling numerical iterations
Posted: Feb 23, 2013 11:31 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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.




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2013. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.