Path: news.mathworks.com!not-for-mail
From: "John D'Errico" <woodchips@rochester.rr.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Speeding up sum of squares
Date: Tue, 6 May 2008 16:26:04 +0000 (UTC)
Organization: John D'Errico (1-3LEW5R)
Lines: 44
Message-ID: <fvq0qr$7tv$1@fred.mathworks.com>
References: <fm012450tjat7jgv5hqhtiokid3ketdsgj@4ax.com>
Reply-To: "John D'Errico" <woodchips@rochester.rr.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1210091164 8127 172.30.248.37 (6 May 2008 16:26:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 6 May 2008 16:26:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 869215
Xref: news.mathworks.com comp.soft-sys.matlab:466950


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