MATLAB Language Performance and Benchmarking Comparing execution time of multiple functions


The widely used combination of tic and toc can provide a rough idea of the execution time of a function or code snippets.

For comparing several functions it shouldn't be used. Why? It is almost impossible to provide equal conditions for all code snippets to compare within a script using above solution. Maybe the functions share the same function space and common variables, so later called functions and code snippets already take advantage of previously initialized variables and functions. Also the there is no insight whether the JIT compiler would handle these subsequently called snippets equally.

The dedicated function for benchmarks is timeit. The following example illustrates its use.

There are the array A and the matrix B. It should be determined which row of B is the most similar to A by counting the number of different elements.

function t = bench()
    A = [0 1 1 1 0 0];
    B = perms(A);

    % functions to compare
    fcns = {
        @() compare1(A,B);
        @() compare2(A,B);
        @() compare3(A,B);
        @() compare4(A,B);

    % timeit
    t = cellfun(@timeit, fcns);

function Z = compare1(A,B)  
    Z = sum(  bsxfun(@eq,  A,B) , 2);
function Z = compare2(A,B)  
    Z = sum(bsxfun(@xor, A, B),2);
function Z = compare3(A,B)  
    A = logical(A);
    Z = sum(B(:,~A),2) + sum(~B(:,A),2);
function Z = compare4(A,B)  
     Z = pdist2( A, B, 'hamming', 'Smallest', 1 );

This way of benchmark was first seen in this answer.