Interpreter. Computational time

Hello, All! We have found some interesting moment. 1. If we use to call next function:
function [result,time] = Func(x,y,u)
N = size(x,1);
M = size(y,1);
result = zeros(M,1);
tic;
for j=1:M
for i=1:N
result(j) = result(j) + u(i) / ( y(j) - x(i) );
end;
end;
time = toc;
we obtain time = 1.9sec.
2. If we use
N = length(x,1);
M = length(y,1);
or use to call next function:
function [result,time] = Func(x,y,u,N,M)
result = zeros(M,1);
tic;
for j=1:M
for i=1:N
result(j) = result(j) + u(i) / ( y(j) - x(i) );
end;
end;
time = toc;
we obtain time = 3.0sec.
3. But if we place the function body into the program body we obtain time = 3.7sec.
4. Besides, if we call this function in a cycle then computational time of each function increased.
Time data averaged by 10 runs for random x,y,u. If we measure time outside the functions (or time of the functions executions) we obtain the same time. Can anybody explain why it is happening?
P.s. We use Matlab 2009b and 2010b and test this on different workstations.

답변 (1개)

Matt Fig
Matt Fig 2011년 5월 12일

0 개 추천

SIZE and LENGTH are not the same thing!
size(rand(2,10000),1)
length(rand(2,10000))
You should show what your data looks like, i.e., what does:
size(x)
size(y)
show?

댓글 수: 4

Dmitry Maryin
Dmitry Maryin 2011년 5월 12일
Yes, you are absolutely right! I cited this (length) as an example of the problem.
Data (x,y,u) looks like:
x = sort(rand(N,1));
And I used:
size(x,1)
Dmitry Maryin
Dmitry Maryin 2011년 5월 12일
size(x,1) show value of N (eg 10000);
size(y,1) show value of M (eg 9999).
Sean de Wolski
Sean de Wolski 2011년 5월 12일
N = length(x,1);
Should throw an error unless you've overwritten function length
Dmitry Maryin
Dmitry Maryin 2011년 5월 12일
Yes, I did mistake when typed. In the code uses: N = length(x);

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

질문:

2011년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by