Speed improvement of the random generator
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(5 个)
the cyclist
2011-4-12
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 个评论
Jan
2011-4-12
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
2011-4-12
If the number of iterations isn't too large, you might save even more time using
tic; x = rand(1,1e5); toc
Oleg Komarov
2011-4-12
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 个评论
Guillaume A.
2011-4-12
1 个评论
Matt Fig
2011-4-12
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
2011-4-12
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 个评论
the cyclist
2011-4-12
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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!