function_handle - Handle to function - MATLAB (original) (raw)

Invoking handles to named functions that are not nested shows improved performance. Invoking such function handles no longer results in an overhead compared to calling functions directly. For example, in a file named myFun.m in your current folder, create the myFun function.

function y = myFun(x) y = x; end

In a file named timingTest.m in your current folder, create a function that invokes a handle to myFun. ThetimingTest function is about 40x faster than in the previous release.

function t = timingTest f = @myFun; n = 1e7; tic for i = 1:n out = f(3); end t = toc; end

The approximate execution times are:

R2022b: 4.4 s

R2023a: 0.11 s

The code was timed on a Windows® 10, Intel® Xeon® CPU E5-1650 v4 @ 3.60 GHz test system by calling thetimingTest function.