Code Optimization and OOP programming
이전 댓글 표시
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 ;
end
But 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 ! :)
댓글 수: 1
Babak
2013년 5월 2일
just FYI, either say OO programming or OOP...
채택된 답변
추가 답변 (2개)
Antoine
2013년 5월 2일
댓글 수: 8
Matt J
2013년 5월 2일
I get Out of memory because NxN is too big for my RAM (to find a contiguous block).
You could try a sparse matrix instead
V.property4=sparse(i,j,s,N,N,nzmax);
Philip Borghesani
2013년 5월 2일
I would make property4 a cell array:
V.property4{i} = cat(2,V.property4{i},smthg) ;
Matt J
2013년 5월 2일
A cell array of length 100000 will have the same RAM scattering problem as an object array of length 100000.
Antoine
2013년 5월 2일
No, modifying things column-wise should be fastest. Make sure you're specifying nzmax. I also hope you're not starting with S equal to all zeros. You should initialize S with as much data as you can, using the syntax,
S=sparse(i,j,s,N,N,,nzmax)
Actually, Philip's suggestion of a cell array might be the best option you have. Every time you insert into a sparse matrix, the table of non-sparse entries has to be resorted...
Maybe you could also store things in a matrix with columns of the form
[idxA, idxB, smthg, zeros(1,pad)].'
If the smthg's are all of similar lengths, this might be too bad a way to encode things.
Antoine
2013년 5월 2일
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!