组织MATLAB类的最佳方法? [英] Best way to organize MATLAB classes?

查看:106
本文介绍了组织MATLAB类的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MATLAB有两种组织课程的方式:

MATLAB has two ways of organizing classes:

@目录:


@ClassName\
    ClassName.m
    Method1.m
    Method2.m

单个文件:


ClassName.m:
classdef ClassName
    methods
        % all methods included here
    end
end

第一种样式在新的classdef语法之前存在,但似乎是一种更结构化的处理方式.第二种样式(所有内容都在一个文件中)是新的.

The first style existed before the new classdef syntax, but seems to be a more structured way of doing things. The second style (everything in a single file) is new.

您使用哪种方法,为什么?

Which method do you use, and why?

推荐答案

新的单文件样式具有一些优点.它允许并鼓励您编写许多小的方法,我认为这些方法会导致代码分解的更好.创建新文件,保存文件并将其添加到源代码管理(我们都使用源代码管理,对吧?)的麻烦很小,但是添加了几十种小方法就足够了它通常使我不愿将类分解为更细粒度的功能.编辑整个类很容易进行浏览,搜索和替换,而不必打开多个单独的编辑器选项卡,然后可以使用这些选项卡来组织不同类的源代码.

The new single-file style has some advantages. It allows and encourages you to write lots of small methods, which I think leads to better-factored code. The hassle of creating a new file, saving it, and adding it to source control (we are all using source control, right?) is minor, but added up over a couple dozen little methods is enough that it usually discourages me from factoring a class into finer-grained bits of functionality. And editing the whole of your class is convenient for browsing, search and replace, and not having to open a dozen separate editor tabs, which can then be used to organize source code for different classes.

对于较大的代码库,单文件样式可能具有性能上的优势.遍历源代码树的源代码控制和部署系统为stat和diff操作之类的文件收取每个文件的费用.例如,对于较大的代码库,数千种方法可能非常重要,尤其是在网络驱动器上.我怀疑使用Matlab编译器部署的应用程序也会对性能产生影响.启动时间随着部署的代码库的大小而增加.由于文件操作(我认为)是单独加密的,因此每个文件的成本中有一部分来自文件操作.我怀疑但尚未经过实验测试,使用单个文件类定义将减少已编译的Matlab应用程序的启动成本.

For larger codebases, there may be performance advantages to the single-file style. Source control and deployment systems that iterate over the source tree have a per-file cost for things like stat and diff operations. For a larger codebase, say, thousands of methods, that can be significant, especially on a network drive. I suspect there's also a performance effect for apps deployed with the Matlab compiler. Startup time increases with the size of the deployed codebase. There is a per-file part of this cost, from file operations, and because the files (I think) are encrypted individually. I suspect, but have not experimentally tested, that using single file class definitions will reduce the startup cost for compiled Matlab apps.

但是,我的大多数代码都使用旧的多文件组织.部分原因是我们的代码库是在几年前才开始使用这种新样式的.但部分是为了性能.新的单个文件组织仅适用于新样式的MCOS Matlab类,并且由于方法分配开销较高,因此它们比旧样式的Matlab类要慢.例如.这是一个基准代码段,显示了什么都不做的nop()方法的执行时间.

However, I use the old multi file organization for most of my code. Partly because our codebase was started a few years back before the new style was commonly available. But partly for performance. The new single file organization only works with the new style MCOS Matlab classes, and they are slower than old style Matlab classes due to a higher method dispatch overhead. E.g. here's a benchmark snippet showing execution time of do-nothing nop() methods.


Calling each function/method 100000 times
nop() function:                 0.02715 sec   0.27 usec per call
nop(obj) method:                0.24629 sec   2.46 usec per call
classdef nop(obj):              0.98572 sec   9.86 usec per call
classdef obj.nop():             1.81307 sec  18.13 usec per call

在进行大量方法调用的代码库中,这可能会对性能产生重大影响. (另请参见 MATLAB是OOP慢还是我做错了什么?)

In a codebase that makes lots of method calls, this can have a significant performance impact. (See also Is MATLAB OOP slow or am I doing something wrong?)

另一个优点是Matlab的自动压痕器将缩进类定义中的每个节和每个方法,因此所有可执行代码的基线是两个制表符,浪费了8列屏幕空间.

One other nit is that Matlab's auto-indenter will indent every section and every method in a class definition, so the baseline of all your executable code is two tab stops in, wasting 8 columns of screen real estate.

总的来说,如果不是出于OO性能的考虑,我可能会使用单个文件,而我正在以这种方式编写新的非性能关键类.

On balance, were it not for OO performance considerations, I'd probably go with single file, and I'm writing new non performance critical classes that way.

更新:它看起来也像一个有用的文档生成器contentsrpt(),不能与在classdef文件中定义的函数一起使用.只有那些在单独的功能文件中.

UPDATE: It also looks like contentsrpt(), a useful documentation generator, does not work with functions defined inside the classdef file; only those in separate function files.

这篇关于组织MATLAB类的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆