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.
David
2010-04-12 20:17:08
Sadly, I don't know of a MATLAB profiler.
rlbond
2010-04-12 22:16:48
@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
2010-04-13 14:12:44
+2
A:
Just create an array of structs and double the size of the array when it hits the limit. This scales well.
Pyrolistical
2010-04-12 20:30:35
How are structs implemented? Does moving the array just mean copying pointers?
rlbond
2010-04-12 22:17:10
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
2010-04-12 22:34:28
@rlbond: Yes, MATLAB structs and cells do not contain data itself but pointers to the data so all operations are cheap.
Mikhail
2010-04-13 09:51:50
+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
2010-04-12 22:44:41