Loren on the Art of MATLAB

Turn ideas into MATLAB

Run Code Faster With the New MATLAB Execution Engine

今天我想向您介绍一位客人博格格,Dave Bergstein,他是Mathworks的Matlab产品经理。今天,Dave将讨论新的Matlab执行引擎。

Contents

重新设计Execution

In R2015b we changed the way all MATLAB code is executed with the introduction of the new MATLAB execution engine. The improved architecture runs MATLAB programs faster and provides a platform for future enhancements. And these benefits come without requiring changes to your code.

See what some customers are saying about the new execution engine:

“我与各种各样的客户一起工作,花时间帮助许多人加快他们现有的Matlab代码。然后,我非常兴奋,然后看看新的执行引擎有帮助性能。一个客户报告了现在的关键性能关键过程R2015B中的速度快两次,为他们的日常运营提供了显着的积极影响。“Yair Altman
Matlab Expert和The Book Accelerating Matlab Performanc(CRC Press)

"The new MATLAB execution engine in R2015 halved the execution time of our MATLAB code."Robert Danforth
Director -“ Engineering, Analytical Modeling & Test Labs, Kohler Co.

"My apps -“ 1D and 2D finite volume for heat transfer and fluid flow -“ perform significantly better -“ about 35% better. Good job!"Enrico Nobile
Professor of Thermodynamics and Heat Transfer, Dept. of Engineering and Architecture, University of Trieste, ITALY

它对您的意义:更好的性能,质量和可扩展性

The new execution engine was designed with performance in mind from the beginning. The following are some specific language areas with the most significant improvements:

  • 函数调用:功能呼叫开销大大减少;将代码组织成许多小功能,不再有明显的表现损失。
  • 面向对象的功能:米any object-oriented operations execute faster.
  • Element-Wise Math Operations:米any element-wise math operations execute faster.

A number of factors account for the speedup in these areas and others, including improved just-in-time (JIT) compilation of MATLAB code. Redesigning the execution engine was also a time to revisit and resolve old language imperfections and bugs, improving quality. And it was an opportunity to design the engine for extensibility, facilitating future enhancements and new features to come.

What is JIT Compilation?

The new MATLAB execution engine can JIT compile all MATLAB code and this is an important factor in how it executes code faster. Programming languages like C++ are so-called compiled languages. C++ code goes through a compile step initiated by the author. This compile step generates machine code which executes directly on the target hardware. And compilers can often optimize the generated code to help applications execute faster.

Interpreted languages are executed command-by-command by an interpreter program and don't require an explicit compile step. For engineers and scientists working interactively with their data, it can be very helpful to execute commands this way, altering the sequence of commands as they work. Although this style of execution can be very effective for exploring ideas, it can be slow when executing blocks of code.

Matlab通过在飞行中编译MATLAB代码或即时编译MATLAB代码,提供两全其美。译成MATLAB代码是否在类,函数,脚本或只是在命令行中。用户没有明确的编译步骤,启动和MATLAB代码可以在一行中以小作为一行的块执行。MATLAB JIT编译器生成本机的机器级代码,该代码针对执行的MATLAB代码和特定的硬件平台进行了优化。

马铃薯草has employed JIT compilation since release R13 in 2002. The original JIT compiler was designed as a supplement to the MATLAB interpreter, and supported a limited subset of the language. The new execution engine was designed with a focus on JIT compilation, where JIT compilation is an integral part of the engine and able to compile the entire MATLAB language.

Example Improvement

This example gives a sense of the benefit of the new JIT compiler. It demonstrates a difference between the previous JIT compiler and the new JIT compiler which can compile all MATLAB code.

The functiontest1.calls the functionfoo1在使用循环变量执行直接计算的for循环中2。The functionfoo1first checks its input value and can issue an error using the错误command if the input is out of range. The error confidition won't occur in this example, but such input checking is a common practice that helps when functions get reused.

functionTest1 x = zeros(1,1e6);forII = 1:1E6 x(ii)= foo1(ii);endendfunctiony = foo1(x)如果(x<0) error('x must be positive');endy = 2*x.^2+3*x+4;end

我记录了以下时间Test1使用时代command on my laptop [1]:

  • Test1with R2015a: 1.7887 seconds
  • Test1with R2015b: 0.0435 seconds

The new execution engine is more than 40 times faster executing this example!

Next, consider what happens when we make a slight change to the test.Test2is the same asTest1除了它呼唤foo2它通过另一个功能发出可能的错误。

functiontest2 x =零(1,1e6);for2= 1:1e6 x(ii) = foo2(ii);endendfunctiony = foo2(x)如果(x <0)显示error;endy = 2*x.^2+3*x+4;endfunctiondisplayerror错误('x must be positive');end

我记录了以下时间Test2使用时代command:

  • Test2使用R2015A:3.8667秒
  • Test2with R2015b: 0.0430 seconds

用R2015ATest2需要两倍的时间才能运行Test1did (3.8667 seconds vs 1.7887 seconds). The reason for the slowdown is that the added code complexity limits the ability of the previous JIT compiler to compile the code. With R2015b the execution time betweenTest1andTest2is nearly the same, demonstrating the ability of the new execution engine to JIT compile all MATLAB code. The result is thatTest2使用R2015B中的新执行引擎更快地执行近90倍。

Improving Performance

马铃薯草spends some amount of time compiling the code. The time to compile is typically small relative to the time to execute the code. And when code is run repeatedly in a MATLAB session without being cleared, you get the greatest benefit since the code only needs to be compiled once. Examples of running code repeatedly are for-loops or rerunning unmodified MATLAB files. Knowing this, consider the following ways to improve the performance of your code:

  1. Avoid using清除allsince it clears the compiled code. Use清除相反,当您想要清除您的数据并避免重新产生Matlab花费编译的时间。
  2. 米odularize your code into multiple files so that when you make edits, it reduces the amount of code that gets recompiled on the next run. Modularizing code into separate functions and classes can also help improve code readability, reusability, and maintainability.

结果

We collected and tested 76 performance-sensitive user applications to evaluate the performance of the new engine. The programs tested range in size from a few dozen lines of code to hundreds of lines of code and cover applications from device simulation to finance. The histogram below shows the number of applications (or tests) that ran some amount faster or slower in MATLAB 15b compared to 15a. From the histogram you can see that there is strong shift to the right, indicating faster performance. These applications ran on average 40% faster with the new execution engine. And over a dozen of the applications ran at least twice as fast.

您的代码有多快?

Test your code with the redesigned MATLAB execution engine by downloading and installing R2015b. You'll be taking advantage of the MATLAB execution engine as soon as you start running your code. For tips on improving code performance, see用于提高性能的技术。要了解有关评估代码性能的更多信息,请参阅衡量程序的表现。并与我们分享您的代码如何通过留下注释来使用新的执行引擎执行这里!

[1] For a great discussion of timing code, see罗兰的12月的博客帖子




Published with MATLAB® R2015b

|
  • print
  • send email

Comments

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