Why is m-function overhead 100 times more than built-in overhead.

11 ビュー (過去 30 日間)
SK
SK 2014 年 9 月 19 日
コメント済み: SK 2014 年 9 月 22 日
function Ignore(a)
end
Then run the following:
tic;
for i = 1 : 200000
Ignore(A);
end
toc;
Elapsed time is 0.945148 seconds. I've found on my system Matlab can do about 200,000 empty function calls per second. So function call overhead is about 5 microseconds.
A = rand(100000, 1);
tic;
for i = 1 : 200000
b = numel(A);
end
toc;
Elapsed time is 0.011971 seconds. So here the number of calls per second is around 20,000,000. This is almost 100 times faster even with a numel() lookup.
I'm curious as to why the m-function overhead is so high. By the way I've also tried putting this code in an m-file and running it. The result is the same.
I've found that with these m-function timings. it is possible for code in a loop to be swamped by overhead time. For example calling a function 10,000,000 times - the function calls 10 other functions which in turn call an average of 5 functions say. This gives 500,000,000 function calls - which would take about 2500 seconds or 40 minutes. If on average each function does only 1 microsecond of work (this is not implausible at all), the actual computation time is only 500 seconds.
Thus: 8 minutes computation + 40 minutes call overhead. Just to heighten the sense of the ridiculous, with a really massive computation, this could translate to an 8 hours computation taking two days.
Could anyone comment on the reason for the high overhead, what to do to mitigate this problem or any other useful information related to this.
An unpleasant surprise in my code (the second in a few days) prompted me to write this. This was the first unpleasant surprise.
The second is the nnz() function. For so long I had always implicitly assumed that it had constant time complexity. I just realized I was wrong and that it has constant time only for sparse matrices.
If nnz() were constant time (Matlab arrays already store other useful information), it would also mean that all(A) and any(A) would be constant time and considering how often these are used, it would be a significant improvement.
Thank you.
[Second loop was edited for typos.]
  10 件のコメント
SK
SK 2014 年 9 月 22 日
Also mex function calls.
tic;
for i = 1 : 200000
MexOverhead(); % Empty mex function
end
toc;
Elapsed time is 0.452725 seconds.
Calling a matlab function (built-in or m-file) from mex is even more expensive (maybe around 100 times more).

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

回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by