Info

This question is closed. Reopen it to edit or answer.

Why does indexing take longer?

1 view (last 30 days)
Larry
Larry on 2 Jul 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
I wrote a search function that the number of loops vary but can be around 30 million. When the function finds a match, it records the information as follows:
spur_table_1(b2_index + 1,:) = [harm_0 harm_1 harm_2 order_c band-1 rx1 rx1 harm_0_nz * freq0 harm_0_nz * freq0 harm_1_nz * freq1 harm_1_nz * freq1 harm_2_nz * freq2 harm_2_nz * freq2];
For 30 million loops the routine takes around 50 seconds to complete.
If I change the command to write to the array individually like shown below:
spur_table_1(b1_index + 1,1) = harm_0;
spur_table_1(b1_index + 1,2) = harm_1;
spur_table_1(b1_index + 1,3) = harm_2;
spur_table_1(b1_index + 1,4) = order_c;
spur_table_1(b1_index + 1,5) = band - 1;
spur_table_1(b1_index + 1,6) = rx1;
spur_table_1(b1_index + 1,7) = rx1;
spur_table_1(b1_index + 1,8) = harm_0_nz * freq0;
spur_table_1(b1_index + 1,9) = harm_0_nz * freq0;
spur_table_1(b1_index + 1,10) = harm_1_nz * freq1;
spur_table_1(b1_index + 1,11) = harm_1_nz * freq1;
spur_table_1(b1_index + 1,12) = harm_2_nz * freq2;
spur_table_1(b1_index + 1,13) = harm_2_nz * freq2;
The routine takes less than two seconds. Why is this?
  1 Comment
Sara
Sara on 2 Jul 2014
Edited: Sara on 2 Jul 2014
To make your life easier, if you do:
new_vect = [harm_0 harm_1 harm_2 order_c band-1 rx1 rx1 harm_0_nz * freq0 harm_0_nz * freq0 harm_1_nz * freq1 harm_1_nz * freq1 harm_2_nz * freq2 harm_2_nz * freq2];
for j = 1:numel(new_vect)
spur_table_1(b2_index + 1,j) = new_vect(j);
end
it is only two times more consuming than its expanded version on my pc.

Answers (1)

per isakson
per isakson on 3 Jul 2014
Edited: per isakson on 3 Jul 2014
I cannot reproduce your results. There is some crucial information missing in your question
  • What is the typical size of the resulting spur_table_1?
  • Is spur_table_1 preallocated?
Matlab arrays use column major order (I failed to find a good reference)
>> M = reshape( (1:12), 3, [] )
M =
1 4 7 10
2 5 8 11
3 6 9 12
.
I made a test that you may want to study. The fastest code is five hundred times faster than slowest. (R2013a,64bit,Win7)
cssm( 1e4 )
ans =
0.2083 0.2051 0.0084 0.0048 0.0072 0.0069 0.0004
and
>> cssm( 2e4 )
ans =
0.8326 0.8087 0.0166 0.0099 0.0142 0.0137 0.0008
The mfile, cssm, is attached.
.
Maybe you could modify cssm.m to reproduce the difference in execution time that you encounter.
  5 Comments
Larry
Larry on 3 Jul 2014
I got the following results when I ran the cssm file:
>> elapse = cssm( 1e4 )
elapse =
0.1240 0.1138 0.0076 0.0040 0.0060 0.0055 0.0003
>> elapse1 = cssm( 1e5 )
elapse1 =
24.4641 24.1554 0.0712 0.0407 0.0590 0.0588 0.0032
Perhaps there's something in the setup of my MATLAB. I have a networked license but I believe the program and execution is on my computer.
per isakson
per isakson on 3 Jul 2014
Edited: per isakson on 3 Jul 2014
Our results with cssm(1e4) show that your computer is a bit faster than mine. I see nothing remarkable in the differences between our runs. (I interrupted my execution of cssm(1e5). I didn't want to wait.)
"Perhaps there's something in the setup of my MATLAB" . Why do you believe that?
.
Does the code in your question resembles any of my seven cases? And
  • What is the typical size of the resulting spur_table_1?
  • Is spur_table_1 preallocated?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!