views:

271

answers:

4

I need to create a queue in matlab that holds structs which are very large. I don't know how large this queue will get. Matlab doesn't have linked lists, and I'm worried that repeated allocation and copying is really going to slow down this code which must be run thousands of times. I need some sort of way to use a growable data structure. I've found a couple of entries for linked lists in the matlab help but I can't understand what's going on. Can someone help me with this problem?

+2  A: 

If you're worried that repeated allocation and copying is going to slow the code down, try it. It may in fact be very slow, but you may be pleasantly surprised.

Beware of premature optimization.

David
Sadly, I don't know of a MATLAB profiler.
rlbond
@rlbond: That is sad indeed. If you want to get to know it, go to the 'Desktop' menu of Matlab, and open the profiler.
Jonas
+2  A: 

Just create an array of structs and double the size of the array when it hits the limit. This scales well.

Pyrolistical
How are structs implemented? Does moving the array just mean copying pointers?
rlbond
As far as I know everything in Matlab is data. So struct is just data. And you don't "move the array". Just double the size of the existing array. `x(2*length(x)) = ...your 'empty' struct`
Pyrolistical
@rlbond: Yes, MATLAB structs and cells do not contain data itself but pointers to the data so all operations are cheap.
Mikhail
+3  A: 

I posted a solution a while back to a similar problem. The way I tried it is by allocating the array with an initial size BLOCK_SIZE, and then I keep growing it by BLOCK_SIZE as needed (whenever there's less than 10%*BLOCK_SIZE free slots).

Note that with an adequate block size, performance is comparable to pre-allocating the entire array from the beginning. Please see the other post for a simple benchmark I did.

Amro
+1  A: 

Well, I found the easy answer:

L = java.util.LinkedList;
rlbond

related questions