Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Re: object.vector(100) = 1
Posted:
Jan 23, 2013 5:51 PM


Am 1/23/2013 7:22 AM, schrieb Steven_Lord: > > > "Peter Mairhofer" <63832452@gmx.net> wrote in message > news:kdn44k$vib$1@news.albasani.net... >> ... does this always reallocate/copy the complete vector although it is >> preallocated? >> >> classdef SampledSignal >> properties (GetAccess = 'public', SetAccess = 'public') >> data; >> end >> >> methods >> function this = set.data(this, vector) >> [...] >> this.data = vector(:); >> end >> end >> >> methods >> function this = SampledSignal(N) >> [...] >> this.data = zeros(N, 1); >> end >> end >> >> x = SampledSignal; >> for n=1:N >> x.data(n) = func(n); >> end >> >> takes forever; > > It calls your set.data method N times. If you had a get.data method as > well, it too would be called N times. If N is large, that could take a > while. > > http://www.mathworks.com/help/matlab/matlab_oop/propertyaccessmethods.html#brh8ht7 > > >> x = SampledSignal; >> data = x.data; >> for n=1:N >> data(n) = func(n); >> end >> x.data = data; >> >> takes "only" 30 seconds on my old machine. >> Why? > > This calls your set.data method 1 time on the last line and a get.data > method (if you have one) 1 time on the second line. > > How large a value of N are you using? How long does 1 call to set.data > take? If you let it run about 30 seconds plus (N1)*(duration of a > set.data call) [for a smaller value of N] does the first code complete > its execution?
Hi,
Thanks for the hint. N=10000 in one case.
EDU>> tic, x.data(1000); toc Elapsed time is 0.000568 seconds. EDU>> tic, x.data(1000) = 1; toc Elapsed time is 0.033976 seconds. EDU>> tic, x.data(1000) = 1; toc Elapsed time is 0.002470 seconds. EDU>> tic, x.data(1000) = 1; toc Elapsed time is 0.004774 seconds. EDU>> 10000*0.004774
ans =
47.7400
EDU>> tic, x.data(1000:1010) = 1; toc Elapsed time is 0.002075 seconds. EDU>> tic, x.data(1000:1010) = 1; toc Elapsed time is 0.002514 seconds. EDU>> tic, x.data(1000:1010) = 1; toc Elapsed time is 0.002514 seconds. EDU>> tic, x.data(1000:1010) = 1; toc Elapsed time is 0.002511 seconds. EDU>> tic, x.data(1000:1010) = 1; toc Elapsed time is 0.002522 seconds. EDU>> tic, x.data(1000:1010) = 1; toc Elapsed time is 0.001552 seconds. EDU>> 0.001552*10000
ans =
15.5200
EDU>>
It's still much more that it actually takes (I just cancel because it takes minutes) but I'll just not use obj.x(n) assignments ...
Thanks Peter



