Speed up access to object property
조회 수: 6 (최근 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.
댓글 수: 0
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!