Matlab making function slow when in for loop
5 views (last 30 days)
Show older comments
I have a function that i wrote that does some kind of regression. The function takes 2 arguments as input, the number of samples and the dimension.
When i run the function alone with inputs, say 500 samples and dimension 20, the code runs in less than 2 seconds, but when i put the function in a for loop and vary the number of samples and the dimension, the code becomes very slow (barely proceeding) and will not even reach the 500 samples with dimension 20.
The code looks as follows:
iteration = 50;
alpha_error = zeros(iteration,iteration);
Noise_A_error = zeros(iteration,iteration);
Noise_Y_error = zeros(iteration,iteration);
for j = 1:iteration
for i=1:iteration
[alpha_error(i,j) ,Noise_A_error(i,j) ,Noise_Y_error(i,j)] = Testing_Function(20*i, 10*j, 0.2 );
i
end
j
end
the code will turn slow when i=13 and j=1. But if i test the function by itself for i = 13 and j =1 it the code will run quickly.
How can i fix that?
0 Comments
Answers (1)
Steven
on 27 Jan 2015
Fast memory access depends on cache, you could get a system monitor and see if you are getting cache misses when you are running the program and try to minimize it. Since I don't know what your function does I have no idea how it is accessing the memory. If you get a test array you will find that it always loads faster if you traverse the rows rather than the columns (or vice versa depending on how its stored in memory and which language you are in). When you load an array it will load a block of memory into the processor, if you continue to use the same parts of the array that are loaded into the processor then you will have a fast access of memory. When the processor has a load instruction and it can't find it in the processor cache then it has to get it from the RAM which takes much longer and is called a cache miss and it loads another block of memory into the processor, the hardware is built this way because most programs use instructions or data that are in the same blocks of memory. It looks like your program creates arrays that have bigger columns than rows some of the time and bigger rows than columns others. If you can make the program traverse the memory in the same way it should run the same, you could rotate your matrices later. You will probably never get consistent results with different size matrices. If you need your algorithm to run very fast, translate it to C-code using a mex function, or even assembly if you need it to run faster. Caching varies from processor to processor, if your processor has very little cache then this could become an issue also.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!