Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to make the function 'norm' treat its input as vectors?
Date: Wed, 13 Oct 2010 06:43:04 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 31
Message-ID: <i93kdo$o8f$1@fred.mathworks.com>
References: <i938ok$smb$1@fred.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1286952184 24847 172.30.248.35 (13 Oct 2010 06:43:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 13 Oct 2010 06:43:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:677969

What do you get when you run this function:

function benchnorm

n = 1e6;
ntest = 100;
time = zeros(ntest,3);
for k=1:ntest
    a=rand(1,n);
    
    tic;
    b = norm(a);
    time(k,1) = toc;
    
    tic;
    b = sqrt(sum(a.*a));
    time(k,2) = toc;
    
    tic;
    b = sqrt(sum(a.^2));
    time(k,3) = toc;
end

time = median(time,1);
fprintf('norm(a)         -> %f\n', time(1));
fprintf('sqrt(sum(a.*a)) -> %f\n', time(2));
fprintf('sqrt(sum(a.^2)) -> %f\n', time(3));

end

% Bruno