File Exchange Pick of the Week

Our best user submissions

Good MATLAB Coding Practices

Brett‘s Pick this week has three parts, and is really a tip of the hat to three authors who have taken the initiative to try to
promote and teach good MATLAB programming practices. In particular, I’d like to acknowledge迈克尔·罗宾斯for“良好的MATLAB编程实践”;Pascal Getreuerfor“Writing Fast MATLAB Code”;和Nico Schlömerfor“在MATLAB中编写干净和快速代码的指南。”

I recall fondly my first attempts at writing a bit of MATLAB code; I believe the year was 1996. I fumbled around, read a few
“Getting Started”documents, and began writing code. I was amazed at how easily I could get MATLAB to do what I wanted it to do! MATLAB is
第四代解释语言,这是相对宽容的;甚至某种程度上,丑陋的代码都设法工作。在
years, as I grew more confident (and competent) with the language, I learned just how poor my coding practices were. And in
some cases, the difference between good code and bad code can be orders of magnitude difference in performance (as measured
按计算时间)。它应该花一些时间来建立对语言的掌握。股息将会到来
not only in improved readability and maintainability, but in improved performance as well. Moments before writing this post,
我回顾了我15岁的密码,有着非常不愉快的经历;它可能出现在编码差的教科书中
实践!

Fortunately, Michael provided two versions of his “Good Programming Practices” (2003); I had a font problem with one, but
能够毫无困难地阅读第二本书。迈克尔的文档提供了有用的宝石的可靠页面。他建议其中
那就是:“查找,prod,sum,cumsum,nan,repmat,reshape,eshos和zeros的创造性使用确实可以帮助矢量化您的代码,
但是,尽量不要使代码过于神秘。执行时间和可读性之间存在权衡。如果您的陈述是
cryptic, either include an equivalent (but slow and easy to read) version in a comment or write a MEX file.” He also suggests
that the user “use PROFILE to identify where you need to work on speed improvements.”

Pascal’s “Writing Fast MATLAB Code” (2009) is more ambitious. In a beautifully formatted document, he lays out 12 chapters
on the topic of better utilization of the MATLAB language:

  • 介绍
  • The Profiler
  • Array Preallocation
  • JIT加速
  • 矢量化
  • 嵌入简单功能
  • Referencing Operations
  • Solving Ax = b
  • Numerical Integration
  • Signal Processing
  • Miscellaneous
  • Further Reading

Nico cites Pascal’s file as an inspiration for his 2009 “Guidelines.” In 34 pages, he spouts some great suggestions “aimed
at MATLAB beginners who already know the syntax but feel are not yet quite experienced with it. Its goal is to give a number
提示使读者能够编写高质量的MATLAB程序并避免常见的错误。”

所有这些文件都值得下载和仔细研究。我希望我在开始时把它们回来了。它的
easier to learn good habits than to break bad ones!

顺便说一句,我们现在使用MATLAB的“入门”视频版本,以及一些其他数学作品的资源,以指导用户更好地编码实践。
我特别建议的是录制的网络研讨会,标题为“加速MATLAB应用程序”。我还建议有动力的读者花一些时间按照链接的链接this doc page on code performance。您将快速学习如何时间和介绍您的代码,以及许多改善性能的技术。

评论?

Published with MATLAB® 7.11

|
  • print
  • send email

评论

To leave a comment, please click这里to sign in to your MathWorks Account or create a new one.