From: Hueihan on 6 Aug 2010 13:29 Hi, all I was trying to optimize the speed of my codes and discovered something interesting.. I tried (1) load a mat file and pass the variables into a function vs (2) pass the filename into the function and load mat files inside the function (1) takes 0.003 sec, almost 30 times faster than (2), which takes 0.086 sec I guess loading mat file outside v.s. inside a function differ in terms of memory allocation, but why does this affect the speed so much ? ==================================== main.m ==================================== a.x = rand(100,1); b.x = rand(100,1); filename = 'structure.mat'; save(filename,'a','b'); clear a b % (1) tic load(filename,'a','b'); pass_structure(a,b); toc % (2) tic pass_matfile(filename); toc ==================================== pass_structure.m ==================================== function pass_structure(a,b) tmp = 1024; for i = 1:length(a.x) for j = 1:length(b.x) dist = abs(a.x(i)-b.x(j)); tmp = min(tmp,dist); end end ==================================== pass_matfile.m ==================================== function pass_matfile(filename) load(filename,'a','b'); tmp = 1024; for i = 1:length(a.x) for j = 1:length(b.x) dist = abs(a.x(i)-b.x(j)); tmp = min(tmp,dist); end end
From: Matt Fig on 6 Aug 2010 13:57 Try this instead: S = load(filename,'a','b'); a = S.a; b = S.b; I think the parser is slowed by your poofing of variables a and b.
From: Hueihan on 6 Aug 2010 14:28 "Matt Fig" <spamanon(a)yahoo.com> wrote in message <i3hidg$c1f$1(a)fred.mathworks.com>... > Try this instead: > > S = load(filename,'a','b'); > a = S.a; > b = S.b; > > I think the parser is slowed by your poofing of variables a and b. right..
|
Pages: 1 Prev: Loading specific variables from multiple '-mat' files Next: USB HID interfacing |