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.

- Program 1 -- Bayes' Rule
- Program 2 -- Relaxation Algorithm
- Program 3a -- Vector Comparison, with Loop
- Program 3b -- Vector Comparison, Vectorized
- Program 4 -- Tic-Tac-Toe

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 |

Windows |
Intel x86 |
1 GHz |
256 MB |

Linux |
Pentium III |
550 MHz |
256 MB |

Solaris |
Sparc |
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 |
MATLAB 6.1 |
MATLAB 6.5 |
Performance Gain |

Windows |
16 min., 0.5 sec. |
2.7 sec. |
` x` 355.7 |

Linux |
38 min., 15.8 sec. |
5.9 sec. |
` x` 389.1 |

Solaris |
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.

function score = Bayes(Seq, Matrix, priorProbability) % BAYES Use Bayes' rule to determine signal probabilities. % % score = BAYES(Seq, Matrix, priorProbability) is the % probability that the signal whose probability is % expressed in Matrix occurs at each position in Seq, % where priorProbability is the probability of seeing % the signal at any position. % Using Bayes' rule: % P(a|b) = P(b|a) * P(a) / P(b), where Pa is the probability % that whatever signal we're looking for occurs at a given % position, Pb|a is the probability that we would see this % specific nucleotide if the signal were here, and Pb is the % unconditional probability of seeing this nucleotide here. % Initially, we'll assume each nt is equally likely. Pb = 1/4; % Initialize storage for the result. score = zeros(1, length(Seq)); lm = length(Matrix); ls = length(Seq) - length(Matrix); for m = 1:ls Pa = priorProbability; k = m - 1; for n = 1:lm nt = Seq(k + n); if (nt > 0) && (nt < 5) PbGa = Matrix(nt, n); Pb = Pa * PbGa + (1 - Pa) * 0.25; Pa = PbGa * Pa / Pb; end end score(m) = Pa; end

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 |