Speed comparison between polyfit and A\y

2 ビュー (過去 30 日間)
Karthik
Karthik 2012 年 6 月 20 日
I have a program where I am fitting a straight line to a set of data repeatedly over a million times. I wanted to get the most efficient possible code for doing this and thought polyfit would be the way to go. Surprisingly I found that polyfit is about 60 times slower than a simple A\y type estimation. I used the code below to test this out. Why is polyfit so slow ?
X = 0:50;
Y = X + 0.3 + randn(size(X));
N = 1000;
tic
for count = 1:N
temp = polyfit(X,Y,1);
end
tm = toc;
disp(sprintf('Polyfit too %g ms per call',tm/N/1e3));
tic
for count = 1:N
temp = [ones(length(X),1) ,reshape(X,length(X),1)] \ reshape(Y,length(Y),1);
end
tm = toc;
disp(sprintf('A\\y too %g ms per call',tm/N/1e3));
The output is
Polyfit took 614.351 micro seconds per call
A\y took 10.2792 micro seconds per call
Karthik
  1 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 6 月 20 日
If you:
>>edit polyfit
You'll see that it's a big fancy wrapper for A\b.

サインインしてコメントする。

採用された回答

per isakson
per isakson 2012 年 6 月 20 日
Study the performance of the code with the function, profile. Thus
profile on
code under test
profile viewer
Note especially that this line is dark red
0.97 1000 72 elseif warnIfLargeConditionNumber(R)
  1 件のコメント
Karthik
Karthik 2012 年 6 月 20 日
Thanks! That makes sense!
Karthik

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by