组织 MATLAB 课程的最佳方式?

发布于 2024-08-26 21:53:06 字数 1431 浏览 10 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦与时光遇 2024-09-02 21:53:06

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

对于较大的代码库,单文件样式可能具有性能优势。迭代源树的源代码控制和部署系统对于 stat 和 diff 操作等操作具有每个文件的成本。对于更大的代码库,例如,数千个方法,这可能很重要,尤其是在网络驱动器上。我怀疑使用 Matlab 编译器部署的应用程序也会产生性能影响。启动时间随着部署的代码库的大小而增加。此成本中有一个每个文件的部分,来自文件操作,并且因为文件(我认为)是单独加密的。我怀疑(但尚未进行实验测试)使用单个文件类定义将降低编译后的 Matlab 应用程序的启动成本。

但是,我的大部分代码都使用旧的多文件组织。部分原因是我们的代码库是在新样式普遍可用之前几年就开始的。但部分是为了性能。新的单文件组织仅适用于新样式的 MCOS Matlab 类,并且由于方法调度开销较高,它们比旧样式的 Matlab 类慢。例如,这是一个基准片段,显示了不执行任何操作的 nop() 方法的执行时间。

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慢还是我做错了什么?

另一个问题是 Matlab 的自动缩进器会缩进类定义中的每个部分和每个方法,因此所有可执行代码的基线是两个制表位,浪费 8屏幕空间的列。

总而言之,如果不是出于面向对象性能的考虑,我可能会使用单个文件,并且我正在以这种方式编写新的非性能关键类。

更新:它看起来也像contentrpt(),一个有用的文档生成器,不适用于classdef文件中定义的函数;仅限于单独的函数文件中的那些。

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.

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.

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

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?)

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.

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.

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.

江南月 2024-09-02 21:53:06

我发现@-directories 是一个混杂的东西(例如公共/私有,那是什么?),最好忘记。在最近的版本中(我相信自 2007b 以来), 组织课程的最佳方式是使用包。这提供了一个更干净的命名空间。我认为在一个文件中处理整个类可以更容易地了解该类正在做什么,并且在重构时减少 1000% 的烦恼(想象一下在更改变量名称后更改 8 个文件)。

I've found the @-directories to be a kludge (e.g. public/private, what's that?) that is best forgotten. In the most recient versions (since 2007b, I believe), the best way to organize your classes is with packages. This gives a much cleaner namespace. I think working with the entire class in one file makes it a lot easier to get a sense of what the class is doing, and 1000% less annoying when it comes to refactoring (imagine changing 8 files after changing a variable name).

末が日狂欢 2024-09-02 21:53:06

我使用单文件方法。我发现当代码由单个文件组成时,组织代码会更容易一些,并且单元格标题使得在方法之间跳转非常容易。另外,如果我创建一个新的@类,我可能需要重新创建路径才能使用它,而我对此没有耐心。

话虽如此,我并不认为单文件风格比多文件风格好多少;拥有大量易于浏览的小文件也可能非常有用。

I use the single file method. I find it somewhat easier to organize the code when it consists of a single file, and cell titles make it very easy to jump around between methods. Also, if I create a new @-class, I might need to recreate the path before being able to use it, and I don't have the patience for that.

Having said all that, I do not think that the single-file style is much better than the multi-file style; having lots of small, easy-to-look-through files might be very useful as well.

撩人痒 2024-09-02 21:53:06

使用 @ClassName 目录的优点是,如果您对 classdef 文件进行任何更改,matlab 会强制您清除并重新加载类。如果将函数的实现放在单独的 m 文件中,并将方法签名放在 classdef 文件中,则可以随意修改实现,而无需清除类。

The advantage of using the @ClassName directory is that matlab forces you to clear and reload a class if you make any changes to the classdef file. If you put the implementation of functions in separate m files and just put the method signatures in the classdef file, you can muck around with the implementation without having to clear the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文