2

In C++ (at least as of a decade ago), there was speed advantage in defining the body of a class method in the header file, where the class is defined. No function call overhead was suffered because, in the compilation process, the invocation of such functions was replaced by the code in the body of the function. Subsquently, all source level optimizations (and all optimizations beyond source level) could be brought to bear.

Is there an analogous advantage to putting the body of class methods in the classdef file itself rather than in a separate m-file? I'm speaking specifically about the case where one defines a @myclass/myclass.m, with method m-files in the directory @myclass. The two options I'm considering is to have the code for the body of a method mymethod put into the classdef in @myclass/myclass.m versus being in a separate file @myclass/mymethod.m.

However, an very related auxiliary question would be how those two options compare with having everything defined in a myclass.m file, with no folder @myclass.

Please note that I have previously posted this to usenet

user36800
  • 2,019
  • 2
  • 19
  • 34
  • [Related](http://stackoverflow.com/a/1745686/2278029), but @AndrewJanke's code doesn't appear to test this case. Also related: [Best way to organize MATLAB classes?](http://stackoverflow.com/questions/2525615/best-way-to-organize-matlab-classes). – horchler Dec 04 '15 at 16:08
  • Well, the thread says that old style classdef directories are faster than new single-file classdefs. It also says that the OO slowness is much ameliorated in 2015b (no results for 2015a, which is what I have). So my question might be answered as best as can be, considering the opaqueness of Matlab OO internals. I wouldn't say that I'm confident enough about that last statement to reflect this in an answer posting, so I'll just wait for a bit. Thank you for pointing me to that thread. – user36800 Dec 04 '15 at 16:28
  • I was wrong. I found the [`classdef` file with functions in the body](https://github.com/apjanke/matlab-bench/blob/master/bench_matlab_ops/dummymcos.m), All of the results prefixed with "classdef" refer to methods in the body of the `classdef`. It appears that separate files is faster in both R2014a and R2015b even if the overall OOP performance has increased. If you really want to know, you can of course [run the benchmark code](https://github.com/apjanke/matlab-bench) on your own machine. – horchler Dec 04 '15 at 16:41
  • I'm a newbie to Matlab's OOP, and trying to belt out project deliverables in the typical crisis timelines....I just wanted to benefit from the prevailing knowledge out there. Perhaps delve into benchmarking when I'm more proficient. Thanks for clarifying. I'll sum it up as a short answer. – user36800 Dec 04 '15 at 19:14

1 Answers1

0

Summarizing the comments as the answer to this question: Using an @classFolder folder containing separate method m-files is faster than having a single m file containing the entireties of the function definitions in the classdef. This is the case even though OOP in general has sped up in 2015b.

I find this a happy answer because I see great value in separating the code implementation of a class's methods from the class definition itself. That's the whole idea of separating interface from implementation. I can look at the classdef and see only a map of the class rather than have those key information elements completely dispersed by the deluge of code that accompanies implementation.

It's just too bad that this doesn't work so well for weakly typed languages. What's listed in the classdef is just member names (properties or methods) with no specification of what class they are. So not as much information as in a strongly typed language. In fact, very little info about what the class, its properties, and its methods really are. Furthermore, there is nothing to ensure that the actual method implementation even complies with the argument list in the classdef. These kind of details helped prevent development errors in a strongly typed language, especially when one's body of classes get large.

user36800
  • 2,019
  • 2
  • 19
  • 34