Programming and Data Types    

Sample Accelerated Programs

The programs in this section should provide some insight into how to make the best use of the MATLAB performance enhancements described in the previous section on Performance Acceleration. Measurements taken on each program show a sizeable performance improvement over earlier versions of MATLAB.

The programs are:

The following table shows hardware specifications for the systems used in measuring performance on the program examples in this section.

Operating System
CPU Type
Processor Speed
Main Memory
Intel x86
1 GHz
256 MB
Pentium III
550 MHz
256 MB
270 MHz
128 MB

Program 1 -- Bayes' Rule

This example uses a program that implements Bayes' rule for computing probability based on prior probability and updated information. The program contains nested for loops that, unless they were vectorized, would be quite costly in execution time without performance acceleration.

The table below compares the performance of MATLAB on this program in MATLAB 6.1 (with no acceleration) and in MATLAB 6.5 (with acceleration). The increase in performance is shown in the far right column for the three operating systems tested.

Operating System
Performance Gain
16 min., 0.5 sec.
2.7 sec.
x 355.7
38 min., 15.8 sec.
5.9 sec.
x 389.1
1 hr., 47 min.,
32.7 sec.
57.9 sec.
x 111.4

Here is the Bayes.m program. Refer to the section What Makes It Faster to see how this program was written to run at optimal speed.

The times shown in the table above were recorded by running the Bayes program on data stored in a double matrix and a large 8-bit integer vector, with a priorProbability of 0.0001.

What Makes It Faster

The program spends most of its time in a nested for loop that calculates the P(b|a), P(b), and P(a) values. When run with the data shown above, the inner loop executes over 18 million times. Because all of the code in the program uses elements of MATLAB that support acceleration, the entire program runs much faster.

Some of the reasons MATLAB accelerates this code are as follows.

Supported Data Types and Array Shapes.   All of the statements within the inner and outer loops use double and 8-bit integer data in scalar, vector, and two-dimensional matrix form. These data types and array shapes support performance acceleration.

Scalar loop indices.   Both for loops operate on a range of scalar values. For example, ls used in this statement is scalar.

Function Calls and Overloading.   Calling functions that are MATLAB built-ins costs very little time in execution, but calling functions implemented in M-files can use up considerable time. This program calls only built-in functions, like zeros and length, with no function calls at all in the nested loops. In addition to direct function calls, there are also no overloaded operations that implicitly call M-file functions.

Conditional Statements.   The one conditional statement, residing in the inner loop, evaluates scalar terms. This statement uses the scalar, nt, as shown here.

Small Array Size.   The second argument, Matrix, is a 4-by-20 array. Arrays that are small in size execute more quickly with acceleration. The overhead of array handling, more noticeable with smaller arrays, is insignificant in accelerated versions of MATLAB.

The third argument, Seq, is quite large and does not speed up much due to its size alone.

  What to Avoid When Running MATLAB Program 2 -- Relaxation Algorithm