Speed up access to object property
7 ビュー (過去 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!