Speed up access to object property

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.

Respuestas (1)

Chidvi Modala
Chidvi Modala el 15 de Abr. de 2020

0 votos

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.

Productos

Versión

R2019b

Etiquetas

Preguntada:

el 9 de Abr. de 2020

Respondida:

el 15 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by