Fastest way to fill in an array
Ältere Kommentare anzeigen
Hi,
I have written the following program, 3 ways to create the same array:
number = 1e5;
tic
A = [];
for i = 1 : number
A = [A i];
end
toc
tic
for i = 1 : number
B(i) = i;
end
toc
tic
C = [];
for i = 1 : number
C(i) = i;
end
toc
Here are the results:
Elapsed time is 11.126158 seconds.
Elapsed time is 0.003447 seconds.
Elapsed time is 0.020941 seconds.
- Why is the first method (A = [A i];) so slow compared to B(i) = i; ??
Why does writing C = []; slow down the computation? (compared to B)
Thanks!
Akzeptierte Antwort
Weitere Antworten (2)
James Tursa
am 24 Jul. 2013
Bearbeitet: James Tursa
am 24 Jul. 2013
The results will be highly dependent on MATLAB version used. Since none of the examples explicitly pre-allocate the result, you are at the mercy of the JIT as to what MATLAB will do with the three methods. On paper, all three methods are pretty much equivalent ... they have to reallocate the memory on each iteration to make room for the next entry. The only difference is a slight syntax difference that either the JIT recognizes or it doesn't. If it recognizes the pattern of what you are doing in the loop, it can pre-allocate the result for you and greatly increase performance time. If it can't recognize the pattern, then you are stuck with the very inefficient as-written reallocation at each iteration. The JIT might even be smart enough to simply calculate the result directly and avoid the loop entirely (which might be the difference between method 2 and 3 in your example). E.g., here are the timings from an older version of MATLAB that does not have the JIT smarts that later versions have:
Elapsed time is 11.726275 seconds.
Elapsed time is 12.153010 seconds.
Elapsed time is 12.048716 seconds.
Basically no difference in the timings because none of the methods is recognized by the JIT as something it can pre-allocate or pre-calculate for you.
Bottom line: Best to explicitly pre-allocate like David has shown you rather than rely on the JIT to recognize the pattern in your loop to do the pre-allocation for you. Even if you know a certain pattern is recognized by the JIT of your version, what will happen to the performance if it ever gets ported to another machine with an earlier version of MATLAB? Well, what I have shown above will happen ... lousy performance. So always explicitly pre-allocate.
4 Kommentare
Martin
am 24 Jul. 2013
As you are in these memory management experiments, note that there can be a significant difference of execution time between initializing with e.g. 0's, and simply (pre-)allocating memory (careful with 2e4x2e4, this could freeze your system if it starts swapping, but I wanted to use large enough arrays [3.2GB] for the difference to be significant):
>> clear all
>> tic ; a1 = zeros(2e4) ; toc % Init.
Elapsed time is 1.383591 seconds.
>> tic ; a2(2e4,2e4) = 0 ; toc % (Pre-)alloc.
Elapsed time is 0.021422 seconds.
James Tursa
am 25 Jul. 2013
Another alternative, if you know you will be filling in the data downstream, is this UNINIT function from the FEX:
It allocates a variable without initializing the data and is very fast.
Martin
am 28 Jul. 2013
dan
am 10 Okt. 2023
Can someone please explain this one then?
tic
Test = zeros(20000);
for i = 1:20000
for j=1:20000
Test(i,j) = i+j;
end
end
toc
tic
Test = zeros(20000);
for i = 1:20000
for j=1:20000
Test(j,i) = i+j;
end
end
toc
Elapsed time is 5.271389 seconds.
Elapsed time is 0.938210 seconds.
2 Kommentare
"Can someone please explain this one then?"
This topic has been discussed fairly regularly:
As far as I understand, the cause is due to the processor reading a whole block of data into its memory cache, which is then very fast to access (faster even than the main memory).
James Tursa
am 10 Okt. 2023
@Stephen23 +1
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!