Path: news.mathworks.com!newsfeed-00.mathworks.com!newscon02.news.prodigy.net!prodigy.net!border1.nntp.dca.giganews.com!nntp.giganews.com!local02.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail
NNTP-Posting-Date: Tue, 06 May 2008 11:52:36 -0500
From: richardstartz@comcast.net
Newsgroups: comp.soft-sys.matlab
Subject: Re: Speeding up sum of squares
Date: Tue, 06 May 2008 09:52:33 -0700
Message-ID: <1v2124dbkuukhhprui7a8ss4vhmtgn3jcg@4ax.com>
References: <fm012450tjat7jgv5hqhtiokid3ketdsgj@4ax.com> <fvq0qr$7tv$1@fred.mathworks.com>
X-Newsreader: Forte Agent 3.3/32.846
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 64
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 67.183.112.173
X-Trace: sv3-Z7nL/OsjX2sCYlgGe62vPwXc6y+WK7fIwwJMo96S/QY6UN54gA4gSRYvw3Zjo61dSGKRVFFX4e1yVt0!JxCbmioZ2SKNDzA8vq8TrEVnuxoi3od/nMTALY3MzQ5FXvJPuSEw6TqZ27+Tr5d3BzdPUfn8whyd!TWvuLCLGmxjb2q2aJ7idq6oc4Vfo
X-Complaints-To: abuse@comcast.net
X-DMCA-Complaints-To: dmca@comcast.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.39
Bytes: 2632
Xref: news.mathworks.com comp.soft-sys.matlab:466959


On Tue, 6 May 2008 16:26:04 +0000 (UTC), "John D'Errico"
<woodchips@rochester.rr.com> wrote:

>richardstartz@comcast.net wrote in message 
><fm012450tjat7jgv5hqhtiokid3ketdsgj@4ax.com>...
>> I have a problem where most of the computational time is spent
>> computing
>> 
>> sum(x.^2)
>> 
>> where x is a vector of length between 100 and 10,000.
>> 
>> It seems I can speed up computation some by using 
>> 
>> x'*x
>> 
>> instead.
>> 
>> Any thoughts or suggestions on further speed improvement?
>> Thanks.
>> -Dick Startz
>
>As always on these problems, its the little things
>that sneak up behind you.
>
>Is x real? Is x a column vector or a row vector?
>
>If you are confident that you know the answers
>to these questions always, then yes, you can
>save some time by using the blas to do this dot
>product.
>
>x = rand(5000,1);
>
>timeit(@() sum(x.*x))
>ans =
>   0.00045792
>
>timeit(@() dot(x,x))
>ans =
>   0.00057047
>
>timeit(@() x'*x)
>ans =
>   7.4318e-05
>
>John

Thank you, John.

I have real column vectors, so it looks like your last suggestion will
speed things up my nearly an order of magnitude. That would be really
great.

If you have a minute, could you explain what it is that @() x'*x does
and why it makes such a difference, or point me to the right stuff to
read?

-Dick Startz

PS If it matters, I'm running 2007B on a dual processor Windows
machine.

PPS What's timeit()?