Arrays in arrays in arrays: access speeds of cell arrays, structure arrays and objects

5 次查看(过去 30 天)
Hello,
I am working with some pretty hefty data structures, namely an array, in which each element is an array, in which each element is an array of objects.
The current implementation is focussed towards tidy and efficient code. Each layer of the pyramid is composed of a cell array of handle objects, whose properties are a cell array for the next layer plus various related statistics, and whose methods are processing steps for that layer. The disadvantage of this implementation is that fetching a property from an object at the deepest level is slow, about 100-200 microseconds.
I would now like to reimplement the code to run quickly, and both intuition and some quick tests show that using simple nested cell arrays or structure arrays will be about 10 times faster. So...
1. Would my original object-based code run faster with value objects instead of handle objects? Would using object arrays make it faster?
2. Is the speed of fetching elements faster from a cell array or a structure array, or are they the same?
3. Am I missing a far more elegant way to solve this problem? (I strongly suspect that I am)
Thankyou! Pete

回答(1 个)

TED MOSBY
TED MOSBY 2024-10-16
Hi Pete,
Here is my opinion on your queries:
Value Objects vs. Handle Objects:
  • Handle Objects: These are reference-based, meaning they point to a memory location where the actual data is stored. This can lead to slower access times as each access involves dereferencing the pointer.
  • Value Objects: These are stored directly, which can lead to faster access times because there is no indirection. However, value objects can increase memory usage, especially if the objects are large, because copies are made whenever the object is passed around or modified.
  • Object Arrays: Using arrays of objects (either handle or value) can improve performance by minimizing the overhead of storing objects in cell arrays. However, the benefit depends on how you access and manipulate the data.
  • Conclusion: Switching to value objects might improve access times if the overhead of dereferencing handle objects is significant. However, the overall impact will depend on how your code is structured and how often you modify the objects.
Cell Arrays vs. Structure Arrays:
  • Cell Arrays: These are flexible and can store different types of data, but accessing elements involves some overhead because of this flexibility.
  • Structure Arrays: These are more efficient if you have a uniform data structure, as they allow direct access to fields without the overhead of cell arrays.
  • Conclusion: If your data can be represented uniformly, structure arrays are typically faster for element access compared to cell arrays. However, if your data is heterogeneous or varies in type, cell arrays provide the necessary flexibility.
Alternative Solutions:
  • Flattening the Structure: Depending on your use case, flattening the data structure could reduce access times. This involves simplifying the hierarchy and reducing the number of nested layers.
  • Using Tables: If your data can be represented in tabular form, MATLAB tables might offer both performance benefits and ease of use.
  • Memory Preallocation: Ensure that you preallocate memory for your arrays and structures to avoid dynamic resizing, which can be costly.
  • Vectorization: Wherever possible, use vectorized operations instead of loops to take advantage of MATLAB's optimized performance for such operations.
  • Parallel Computing: If your processing steps can be parallelized, consider using MATLAB's parallel computing capabilities to speed up the execution.
Hope this helps!
  3 个评论
TED MOSBY
TED MOSBY 2024-10-16
编辑:TED MOSBY 2024-10-16
By overhead i meant, when there is diff types and sizes of data in the cells.
Secondly, each cell can point to diff memory location but sturctured arrays are contiguous, so performance difference lies not in calling the function but the data structure handling.
Thirdly, structure arrays require each field to be of the same type across all elements in the array. While you can store different types by using cells or other structures within fields, but this adds complexity.
Stephen23
Stephen23 2024-10-16
编辑:Stephen23 2024-10-16
"By overhead i meant, when there is diff types and sizes of data in the cells."
Exactly the same applies to structure arrays.
"Secondly, each cell can point to diff memory location but sturctured arrays are contiguous..."
Hummm, while the structure array itself may be contiguous in memory, all of the field content are not:
Exactly the same applies to cell arrays: while the cell array itself may be contiguous in memory, all of the cell content are not:
"...so performance difference lies not in calling the function but the data structure handling."
As the MATLAB documentation makes very clear, structure arrays and cell arrays are both container arrays. They both contain non-contiguous data. They are both indexed using the same functions. It is unclear why you think that acessing structure arrays would be "typically faster" than accessing other types of container arrays, nor have you shown any tests for this. Your explanation so far does not match how MATLAB structures actually work.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by