In order to reduce the number of source files, improve readability of my code and avoid having subfolders, I define several static methods within the same class. Is this going to slow down the runtime? I know that calling methods on objects is expensive. I am just using static methods.
-
1I wouldn't worry too much about it. Only if you plan to use very small (fast) methods in loops with very high number of iteration, your execution time may take a hit yes. If you only call your methods once in a while, and/or if their execution time is high (long computations), then the overhead will be unnoticeable. In any case, only profiling and timing your specific project will give you a definitive answer. – Hoki Feb 08 '15 at 17:03
2 Answers
Calls to MATLAB static methods are even slower than public or private methods. Profile your code before you intend to change anything. If a function/method is not called really often, as @hoki states,
the overhead will be unnoticeable
For performance critical calls I recommend to
use subfunctions or mfile functions.
Try to avoid many function calls
If you require things to be computed in MATLAB and cannot out source critical parts to MEX code, try to bundle everything related to an mfile including subfunctions to structure the code.
If you cannot avoid method calls, use the
my_method(obj, ...)
syntax instead of theobj.my_method(...
notation
Check out this helpful answer on this topic given by @andrewjanke
Here's a run of the benchmark provided in the linked answer with MATLAB 2014b
. The results barely vary.
Matlab R2014b on PCWIN64
Matlab 8.4.0.150421 (R2014b) / Java 1.7.0_11 on PCWIN64 Windows 7 6.1 (VGFDST01)
Machine: Core i7-3770 CPU @ 3.40GHz, 16 GB RAM (ESPRIMO P510)
nIters = 100000
Operation Time (µsec)
nop() function: 0.11
nop() subfunction: 0.11
@()[] anonymous function: 0.54
nop(obj) method: 3.91
nop() private fcn on @class: 0.15
classdef nop(obj): 5.08
classdef obj.nop(): 9.41
classdef pivate_nop(obj): 5.01
classdef class.static_nop(): 10.05
classdef constant: 4.83
classdef property: 1.25
classdef property with getter: 17.56
+pkg.nop() function: 3.57
+pkg.nop() from inside +pkg: 3.18
feval('nop'): 2.27
feval(@nop): 0.21
eval('nop'): 42.72
Java obj.nop(): 19.65
Java nop(obj): 3.85
Java feval('nop',obj): 7.93
Java Klass.staticNop(): 8.40
Java obj.nop() from Java: 0.01
MEX mexnop(): 0.83
builtin j(): 0.02
struct s.foo field access: 0.10
isempty(persistent): 0.00
-
1Thank you for your reply. As I emphasized the methods are static i.e. they are not invoked upon objects. I never have anything like: obj.my_method(... or my_method(@obj .. since I do not have any objects. A static method (like non-static ones) can be either public or private. – Leila Feb 08 '15 at 17:41
-
1yeah, right. I didn't think about that, @seeda. Anyway, it's about 100 times slower than calling a mfile or subfunction, but you will only suffer if you have ~millions of calls. Also too many methods in a file is kind of inconvenient, too. I have a class with 75 methods (including get methods) and the goto list is just to long... – embert Feb 08 '15 at 18:09
Calling static methods used to be expensive, but as of R2015b, it is (almost) just as fast as calling regular .m
functions. So given the convenience they provide in certain cases, there's no more reason not to use them, even in performance-critical parts of your code.

- 1
- 1

- 3,877
- 2
- 19
- 28