From: Juliette Salexa on 9 Aug 2010 15:58 Hello, The profiler says that this line of my code is taking up 50% of the time, (it's called many times in a forloop): A=reshape(bsxfun(@times,reshape(sum(reshape(A,[],N^2),2),1,[]),I),[],1); where size(I)=size(A) = [N^12 , 1] Someone (Walter Roberson) once said on this Newsgroup that executing many functions on one line like this is slower than doing them on separate lines, because the JIT compiler finds it more difficult to optimize the code if it's all done on one line. My concern is that splitting this one-line computation into separate lines would involve making new variables, and I don't have enough memory for this. Is there a way to speed up a line like this without increasing the amount of memory used ? I remember reading a blog post by Loren Shure which taught us how to call functions with input variables without duplicating the memory used, but I search and can't find it anymore. Any discussion about this would be very appreciated, Juliette
From: us on 9 Aug 2010 16:15 "Juliette Salexa" <juliette.physicist(a)gmail.com> wrote in message <i3pmkc$oj5$1(a)fred.mathworks.com>... > Hello, > > The profiler says that this line of my code is taking up 50% of the time, (it's called many times in a forloop): > > A=reshape(bsxfun(@times,reshape(sum(reshape(A,[],N^2),2),1,[]),I),[],1); > > where size(I)=size(A) = [N^12 , 1] > > Someone (Walter Roberson) once said on this Newsgroup that executing many functions on one line like this is slower than doing them on separate lines, because the JIT compiler finds it more difficult to optimize the code if it's all done on one line. > > My concern is that splitting this one-line computation into separate lines would involve making new variables, and I don't have enough memory for this. > > Is there a way to speed up a line like this without increasing the amount of memory used ? > > I remember reading a blog post by Loren Shure which taught us how to call functions with input variables without duplicating the memory used, but I search and can't find it anymore. > > Any discussion about this would be very appreciated, > Juliette a hint: - we simply look at things with the help of... help profile; us
From: Walter Roberson on 9 Aug 2010 16:15 Juliette Salexa wrote: > The profiler says that this line of my code is taking up 50% of the > time, (it's called many times in a forloop): > > A=reshape(bsxfun(@times,reshape(sum(reshape(A,[],N^2),2),1,[]),I),[],1); > where size(I)=size(A) = [N^12 , 1] Could you confirm that that size() you gave is N^12 and not N^2 ? That you are, in other words, expecting that innermost reshape to result in a matrix which is N^10 by N^2 and that summing along the second dimension would thus result in a column vector of N^10 elements? Which you then flip over to be a row vector 1 x N^10 and use bsxfun to produce the product of all pairs of that row vector together with the column vector I which is N^12 x 1, thus producing a matrix which is N^12 by N^10 ? and then you convert the result into a single column vector? If so, then I'm not surprised that it is taking a long time for any non-trivial N. In your "for" loop, what is varying? I'm wondering about the possibility of pre-calculating some of the results. Also, is that exact output order important? If not, then it might be possible to eliminate one of two of the transposes.
From: Sean on 9 Aug 2010 16:17 "Juliette Salexa" <juliette.physicist(a)gmail.com> wrote in message <i3pmkc$oj5$1(a)fred.mathworks.com>... > Hello, > > The profiler says that this line of my code is taking up 50% of the time, (it's called many times in a forloop): > > A=reshape(bsxfun(@times,reshape(sum(reshape(A,[],N^2),2),1,[]),I),[],1); > > where size(I)=size(A) = [N^12 , 1] > > Someone (Walter Roberson) once said on this Newsgroup that executing many functions on one line like this is slower than doing them on separate lines, because the JIT compiler finds it more difficult to optimize the code if it's all done on one line. > > My concern is that splitting this one-line computation into separate lines would involve making new variables, and I don't have enough memory for this. > > Is there a way to speed up a line like this without increasing the amount of memory used ? > > I remember reading a blog post by Loren Shure which taught us how to call functions with input variables without duplicating the memory used, but I search and can't find it anymore. > > Any discussion about this would be very appreciated, > Juliette What size does this line result in? >>reshape(sum(reshape(A,[],N^2),2),1,[]) I've been trying it and ending up with scalars which removes the need for bsxfun entirely. I may be (am probably ;-) ) creating bad data though. Can you produce a small amount of sample data?
From: Walter Roberson on 9 Aug 2010 16:25 Juliette Salexa wrote: > My concern is that splitting this one-line computation into separate > lines would involve making new variables, and I don't have enough memory > for this. > > Is there a way to speed up a line like this without increasing the > amount of memory used ? Each intermediate step in a computation creates an (unnamed) variable that is used for the computation purposes. If that variable is the result of the last step in the computation and it is being assigned to a plain variable, then any previous plain variable by that name is discarded and the name is associated with the header for that unnamed variable _without copying the data_. If the unnamed variable is not the last step in the computation, then at some point (probably before the next line) the unnamed variable is released. Thus, there is virtually no cost to creating a temporary variable of intermediate results and clear'ing that variable once it is no longer needed. Trivial extra memory is used: all that happens is that a name slot gets associated with the data block that is already there anyhow. The one disadvantage of this approach is that sometimes Matlab has fast routines for some more complex operations -- for example A*B+C can be done more quickly than T1 = A * B; T2 = T1 + C; clear T1
|
Next
|
Last
Pages: 1 2 3 Prev: remove a specific frequency from emg signal Next: remove a specific frequency from emg signal |