Struct array wrapped in object, performance issues
显示 更早的评论
I run simulations using several objects and need those objects to store results in one place. I decided to go with a struct array. I thought it would improve performance if I make the struct a property of a handle object. My reasoning was if I pass the struct array around to objects it would be copied every time and reduce performance. I thought if I make it a property in a handle object, it would not be copied if I pass the object around. I ran into some interesting performance issues though and would appreciate any input. I use R2011b.
Here is a simplified class definition for the data storage object with only the construction method and a couple of properties: Pstruct will be a struct array for storing data and PNum is just a double.
classdef anobj < handle
properties
PStruct
PNum=1;
end
methods
function obj = anobj()
end
end
end
Here is a script filling the structure in an object with 1’s which is pretty fast:
clear all
a = anobj(); % an object
b = anobj(); % another object
ntrials=10; niterations=1000;
a.PStruct(ntrials,niterations).field1=0; % 'initialize' the struct array
for t=1:ntrials
tic;
for i=1:niterations
a.PStruct(t,i).field1=1; % store data
end
toc;
end
yielding:
Elapsed time is 0.001008 seconds.
Elapsed time is 0.000967 seconds.
Elapsed time is 0.000972 seconds.
Elapsed time is 0.001206 seconds.
Elapsed time is 0.000992 seconds.
Elapsed time is 0.000981 seconds.
Elapsed time is 0.000975 seconds.
Elapsed time is 0.001072 seconds.
Elapsed time is 0.000951 seconds.
Elapsed time is 0.000994 seconds.
When instead I use a property of another object (=1 as well), changing the line within the loops to:
a.PStruct(t,i).field1=b.PNum; % store data
I get:
Elapsed time is 0.112418 seconds.
Elapsed time is 0.107359 seconds.
Elapsed time is 0.118347 seconds.
Elapsed time is 0.127111 seconds.
Elapsed time is 0.138606 seconds.
Elapsed time is 0.152675 seconds.
Elapsed time is 0.162610 seconds.
Elapsed time is 0.172921 seconds.
Elapsed time is 0.184254 seconds.
Elapsed time is 0.190802 seconds.
Not only performance is orders of magnitude slower, but also there is a very clear trend (verified more generally) of slowing down with each trial. I don’t get it. Furthermore, if I instead use a standalone uninitialized struct array which is not wrapped in an object (this line replaces the one within the loops):
PStruct(t,i).field1=b.PNum; % store data
I get ok performance with no trends:
Elapsed time is 0.007143 seconds.
Elapsed time is 0.004208 seconds.
Elapsed time is 0.004312 seconds.
Elapsed time is 0.004382 seconds.
Elapsed time is 0.004302 seconds.
Elapsed time is 0.004545 seconds.
Elapsed time is 0.004499 seconds.
Elapsed time is 0.005840 seconds.
Elapsed time is 0.004210 seconds.
Elapsed time is 0.004177 seconds.
I like the convenience of data storage object, but it adds huge overhead to execution time due to some weird interaction between struct arrays and objects. Does anybody know what is happening and how to fix this? Thanks.
回答(2 个)
My reasoning was if I pass the struct array around to objects it would be copied every time and reduce performance.
This assumption is wrong. You can enable a debug mode to see, if the data are copied or if more efficient shared data copies are created:
format debug % [EDITED], not "feature"!
Matlab uses a copy-on-write strategy, which provides input data as shared data copy until the first modification of the data appears. E.g. for structs this is very efficient, when only a specific field is changed, while all others are kept.
4 个评论
per isakson
2013-8-5
编辑:per isakson
2013-8-5
Maybe this is useful
feature(‘MemStats’), feature(‘DumpMem’), feature(‘ProcessMem’) – these
are memory reports that are even recommended by official MathWorks tech
notes (1,2), newsletter and technical solutions (1,2). Numerous
references to these features can be found online.
matfan001
2013-8-5
Jan
2013-8-5
How strange! I have edited this minutes after the posting to change "feature debug" to "format debug". Obviously I made something wrong.
类别
在 帮助中心 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!