Speed up access to object property

11 次查看(过去 30 天)
I am storing large amounts of data in an object property. Subsequently I need to run some calculations on that data that I do not believe can be vectorized. Accessing the data in the object takes about 10x-100x as much time as accessing local data. I do not want to create a local copy as the dataset is several GB in size. Is there a way to speed up accessing the data in the object?
A quick example of the problem is here:
classdef storage < handle
properties
data
end
methods
function obj = storage(len)
obj.data = ones(1, len);
end
end
end
clear all;
len = 10000;
localdata = ones(1, len);
obj = storage(len);
tic
for i=2:len-1
localdata(i) = localdata(i-1)+localdata(i+1);
end
toc
tic
for i=2:len-1
obj.data(i) = obj.data(i-1)+obj.data(i+1);
end
toc
Elapsed time is 0.000782 seconds.
Elapsed time is 0.012178 seconds.

回答(1 个)

Chidvi Modala
Chidvi Modala 2020-4-15
One workaround would be, one can recast the object to a struct type in the storage class as A= struct(obj); and perform the property access. This is not as fast as accessing local data. But reduces time to some extent compared to objects.

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by