| Author |
Comment/Response |
Matt Labrum
|
03/23/08 00:43am
Hello,
I have put together the following code for some number crunching work I'm doing, and I am trying to figure out why it is running so slowly.
-----Begin Code-----
RandPoi[\[Mu]_] := RandomInteger[PoissonDistribution[\[Mu]]]
extTimes[T_, \[Mu]1_, \[Mu]2_, s_, \[Lambda]_, q0_, N0_, runs_] :=
Module[{N1, N2, tmpN1, tmpN2, lstTimes},
lstTimes = Reap[
For[i = 0, i < runs, i++,
N2 = Round[N0*q0];
N1 = N0 - N2;
For[j = 1, j <= T, j++,
tmpN1 = 0;
tmpN2 = 0;
For[k = 0, k < Max[N1, N2], k++,
If[k < N1,
tmpN1 += RandPoi[(1 - \[Mu]1)*(1 + s)*\[Lambda]];
tmpN2 += RandPoi[\[Mu]1*(1 + s)*\[Lambda]]
];
If[k < N2,
tmpN1 += RandPoi[\[Mu]2*\[Lambda]];
tmpN2 += RandPoi[(1 - \[Mu]2)*\[Lambda]]
]
];
N1 = tmpN1;
N2 = tmpN2;
If[Or[N1 + N2 >= 10*N0, j == T],
Sow[T];
Break[]
];
If[N1 + N2 <= 0,
Sow[j];
Break[]
]
]
]
][[2]][[1]];
Return[lstTimes]
]
-----End Code-----
Running the command:
Timing[output =
extTimes[100000, 0.002, 0.002, 0.02, 0.9825, 0.75, 1000, 20]]
gives the following output (for example):
{24523.4, {622, 319, 623, 100000, 100000, 2459, 1163, 1070, 381,
100000, 1259, 100000, 100000, 1117, 329, 264, 100000, 309, 568,
300}}
Note that the running time is approximately 6.8 hours. Everything in each of the loops seems to be simple arithmetic, so I am not seeing why the execution time should be so long.
I have coded the same thing in C++ and when I use the same set of parameters, I get very similar results, but the total execution time is around 5 minutes.
If comp specs matter much, I'm on a MacBook Pro running Mac OS X Version 10.5.2 with a 2.6 GHz Intel Core 2 Duo processor and 2 GB 667 MHz DDR2 SDRAM. I'm using Mathematica 6.0.2.0 for Students which is running as an Intel process (with MathKernel running as an Intel (64 bit) process).
Thanks,
Matt
URL: , |
|