Static vs Object Method Performance Considerations

Are there any performance considerations regarding Static vs. object methods in MATLAB OOP?

6 Comments

To clarify, you have a method that could be static, but you are thinking of passing it the object, and then not using the object?
Hi Dan
I just want to know if there is any performance benefits "under the hood". Do static methods run faster than instants methods or visa versa?
Strange things happen in MATLAB OOP
For example consider if you're going to call "getc()" function 10,000 times:
classdef test < handle
properties(Constant)
f = [292.2392340;2340234.947097;103049.1398;12049.0989];
end
methods(Static)
function c = getc1()
c = [1 1 1 1]*test.f;
end
function c = getc2()
ff = [292.2392340;2340234.947097;103049.1398;12049.0989];
c = [1 1 1 1]*ff;
end
end
end
getc2() is about 30% faster than getc1(). I would assume that getc1() would be faster than getc2() since it is reallocating f each call.
Of course I can create some little test like this for static vs instance method invocation performance (and i have) but I just wondered if any folks have non-obvious considerations regarding the matter.
The example is so tiny, I can't imagine that you could get any meaningful timing comparison out of it. Also getc1 could be expected to be slower because it has to parse the static function call test.f
Aside from these observations, if you're doing something where the speed of a function call is the bottleneck, you're probably coding things the wrong way. The idea of MATLAB is you try to vectorize computations and deploy them in just a few function calls, rather than through many repeated function calls.
Hi Matt
It's not clear why getc1 should be "slower since it has to parse the static function call test.f".
the point of the example i gave is to determine if it's faster to allocate on-the-fly or declare something constant. It's unambiguous, if you execute the example 10,000 times that it's much better not to use constant declarations in MATLAB OOP. Tiny test are good b/c they limit the factors at play.
Thanks for your additional comments about "the idea of MATLAB" and my "wrong" coding practices. You'll learn eventually, after you've used MATLAB long enough, that you can't vectorize everything. ;)
@Matt J, It used to be that vectorization was the be all and end all of MATLAB since loops were slow. For over 10 years now MATLAB has had a JIT accelerator which often makes loops the same speed (and sometimes faster) than vectorized code.
Matt J
Matt J on 19 Oct 2012
Edited: Matt J on 19 Oct 2012
@Abel - the problem with your test, though, is that even though you've declared your data constant, you have to execute the command "test.f" to access the data. This is something like calling an mfile function, I'd assume, and there is overhead associated with that. When the data you're processing is small in comparison to the overhead needed to access it, you get misleading results. I believe you even encountered the same issue here
@Daniel - the JIT has its benefits, bit it hasn't leveled the playing field completely. It will always be true, I think, that in a loop, if a function is given too small a chunk of data to process, the overhead from just launching the function (repeatedly) will kill you.

Sign in to comment.

Answers (1)

Dave Foti who manages the OOP group at TMW has a post on Loren's blog that touches on some of these issues.
For your example I am not sure how smart the MATLAB JIT accelartor is and when things get reallocated and refreshed. It seems to me that once getc1 and getc2 are loaded into memory (remember MATLAB parses and loads functions into memory), that they are identical.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Asked:

on 18 Oct 2012

Community Treasure Hunt

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

Start Hunting!