Speed up access to object property

7 views (last 30 days)
Sergio Lucato
Sergio Lucato on 9 Apr 2020
Answered: Chidvi Modala on 15 Apr 2020
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.

Answers (1)

Chidvi Modala
Chidvi Modala on 15 Apr 2020
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.

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!