Speed improvement of the random generator
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I've built a Monte Carlo program, very simple, and I want to improve the CPU time because as soon as I increase the number of simulations, the time exponentially exploses. I've run a profiler on it, and suprisingly, most of the time (>60%) is spent in the generation of the random numbers. If there is a way to reduce this computing time, it would improve a lot the speed of my program.
Any suggestion ?
0 Kommentare
Antworten (5)
the cyclist
am 12 Apr. 2011
It would be best if you could post a snippet of code that illustrates the problem.
My guess is that it is not really the random number generation that is slow. Could it be that, instead, you are growing an array by appending a random number to it every iteration of a loop? Preallocating that array would speed it up.
Also, could you pregenerate all your random numbers first, in a vectorized way, then access them later as you need them?
2 Kommentare
Jan
am 12 Apr. 2011
I agree: exponential slowdown is often a hint to a missing pre-allocation. Try this:
tic; x=[]; for i=1:1e5; x(i)=rand; end; toc
tic; x=zeros(1,1e5); for i=1:1e5; x(i)=rand; end; toc
Andrew Newell
am 12 Apr. 2011
If the number of iterations isn't too large, you might save even more time using
tic; x = rand(1,1e5); toc
Oleg Komarov
am 12 Apr. 2011
Even generating all the random numbers at once (moving it outside of the loops the gain is small), but the difference lies in the legacy mode:
k = 20;
NbTraj = 10000;
NbPas = 100;
tic
s = RandStream('mcg16807','Seed',100);
dW = randn(s,NbTraj,5,NbPas,20);
toc % Elapsed time is 8.288121 seconds.
% No legacy mode
tic
dW = randn(NbTraj,5,NbPas,20);
toc % Elapsed time is 2.695166 seconds.
2 Kommentare
Guillaume A.
am 12 Apr. 2011
1 Kommentar
Matt Fig
am 12 Apr. 2011
Also needed is the sizes of the arrays involved. Perhaps put a call to the WHOS function after these loops then show the output.
the cyclist
am 12 Apr. 2011
Crap. I accidentally just deleted a whole answer instead of a comment I made. Sorry!
One more suggestion. You should be able to pull the random number generation outside of at least one of those loops, assuming it does not use too much memory to do so. That should give you some more speedup.
3 Kommentare
the cyclist
am 12 Apr. 2011
Be aware that your code, with the parameters you have put in, is generating 100,000,000 random normals (in about 10 seconds on my machine). That is by no means "slow". Fully vectorized [r=randn(1.e8,1)] takes 7 seconds. So, I would say you need to find another way!
Siehe auch
Kategorien
Mehr zu Pulsed Waveforms finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!