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

7 ビュー (過去 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 日
Thee "surprise" was that m-function call overhead was so high. I compared it to built-in to highlight how slow it is. Perhaps if I had an idea of what goes into an m-function call, I may have phrased it differently.
The function call overhead in C or C++ with say a single argument is a few cpu instructions, typically taking a couple of nanoseconds. I believe measuring function call overhead in Java is complicated due to various run-time optimizations applied by the JVM, but see for example this link. How does that square with overhead of builtin's in Matlab (around 50 nanoseconds on my machine) and m-function overhead (around 5 microseconds on my machine).
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 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by