|
Hello Alex,
putting the profiler issue aside for a moment and assuming that you want to speed up your function: I think you can improve by a few percent by rewriting the code in your first post like this:
function sum = sum_from_int_im(int_im, rect)
%SUM_FROM_INT_IM Summary of this function goes here
% Detailed explanation goes here
x = rect(1)-1; y = rect(2)-1; w = rect(3); h = rect(4);
sum = int_im(y+h, x+w);
if x>0
if y>0
sum = sum + int_im(y, x);
end
sum = sum - int_im(y+h, x);
end
if y>0
sum = sum - int_im(y, x+w);
end
This is untestet of course, but shoud be equivalent. You can also save a bit more time by immediately computing x+w instead of w, and y+h instead of h.
When using the profiler myself, I have made the following observations that might be interesting for you:
1. In many cases, the profiler seems to have an 'off by one' error regarding the runtimes. This might be inaccurate, but it can also reflect the real behaviour of the interpreter: For instance, if you see a complicated expresssion followed by a line with only 'end', and the profiler reports a long time for 'end', it can be due to lazy evaluation. The interpreter probably postpones the calculations until the latest point where they are necessary regarding the program structure.
2. Even if the profiler provides good hints, you must test what is computational expensive by modifying your program, because optimization techniques in the interpreter make the runtime behaviour of even simple programs complex.
3. The way we usually write MATLAB functions means that an important line is missing in the source code, because it is optional, which is very specific to MATLAB. I mean the 'end' at the end that would make a function definition complete in other languages. Due to the cleaning up, this 'end' can be really expensive, but what should the profiler do if it has no source code line to show the associated time? Adding the optional 'end', possible but rarely seen in MATLAB code, probably makes it visible.
HTH,
Heinrich
|