Hello :) ,
I have problems of runing time with the MatLab program I am working on, I hope someone could be able to give me a hand !
I am working with several classes and trying to use the power of OOP programming. But the size of the data I am working on is quite significant and I create huge arrays of classes object.
Before I show you part of the code in details, I explain that I am modestly familiar with the notion of MatLab code optimizing: I use the profiler to spot the slowness of my code, I pre-allocate my memory and I know what vectorization is.
I have a class called Vertix that has 6 properties, and I create an array V containing N "Vertix". N is big (around 100.000) ... Creating it and filling it is pretty fast (a few seconds), the problem is when I want to modify the following way.
I have :
vertices2change = ... ; % something small of 1xn size, like [ 3 5 7 ]
I want to access V(3), V(5) and V(7) to modify one of their properties. So I do :
for i=1:size(vertices2change,2)
V(vertices2change(i)).property = smthg ;
endBut this loop, of course, is VERY slow. Since I use it a lot in my code, this make my code run in hours, and according to the profiler, this is 99% the fault of this line...
The question is : how do I vectorize this line to make it faster ?
I already try the following but MatLab doesn't like it :
>> V(vertices2change).property = smthg Insufficient number of outputs from function on right hand side of equal sign to satisfy overloaded assignment.
Actually, the answer of
>> V(vertices2change).property
correspond to V(vertices2change(1)).property. Weird right ? At least there is something I didn't get !
I hope I was clear in my explainations and that my question wasn't stupid.
Thanks in advance ! :)
No products are associated with this question.
But this loop, of course, is VERY slow.
Its not clear to me why it should be super-slow. Not if vertices2change consists of only 3 elements. Is 'Vertix' a handle class?
Anyway, it is a bad idea to work with object arrays of lengths like 100000. Same thing with structs. It scatters your data discontiguously across RAM, making it slow to access. It is better to combine your property data from the different V(i) into one matrix and make that a property of a scalar object V.
See also this recent, related thread,
It is slow because I use it a lot of time and vertices2change is in practice way bigger than 1x3, I just wanted to say that it was small compare to the size of V (hundred thousand). But I think you are right, it is slow to access the data because of the discontinuity of the RAM zones. It could be more clever to make arrays of properties directly.
So I would have something like this ?
classdef Vertix < handle
properties
property1 ;
...
property6 ; % let's imagine that property6 has 3 components
end methods
% constructor, more of an "allocator"
function V = Vertix(N)
V.property1 = zeros(1,N) ;
...
V.property6 = zeros(3,N) ; % that would make array of array...
end
end
endAnd then in the code to fill the data :
for i = 1:N
V.property1(i) = smthg ;
...
V.property6(1,i) = smthg ;
endIn this way, I would solve my first problem by writing
V.property(vertices2change) = smthg ;
And it should be way faster !
Ok thank you for the response, I will modify my code right away.
0 Comments