coder.perfCompare
Compare execution times of MATLAB code and code generated using multiple configuration objects
Since R2024b
Syntax
Description
compares the execution times of the MATLAB® functions specified by t = coder.perfCompare(fcnName,numOutputs,runtimeArgs)fcnName with the generated MEX code.
Use runtimeArgs to specify the run-time input arguments used to compare
the performance, and numOutputs to
specify the number of output arguments.
By default,
coder.perfCompareuses a MEX configuration object withIntegrityChecksandResponsivenessChecksproperties set tofalse.coder.perfComparecompares the performance for multiple runs and returns the median of execution times.coder.perfComparefunction uses internal heuristics to determine the number of runs.coder.perfCompareexcludes the timing overhead incurred by the data transfer between MATLAB and generated code execution.
compares the execution times of MATLAB code and the code generated using configuration objects specified by
t = coder.perfCompare(fcnName,numOutputs,runtimeArgs,coderConfigs)coderConfigs. For build types lib and
dll, generated code is executed using software-in-the-loop (SIL) or
processor-in-the-loop (PIL) simulation, which requires an Embedded Coder® license.
specifies additional options using name-value arguments.t = coder.perfCompare(___,Name=Value)
Examples
Compare the execution times for the MATLAB® function integralExpLn and its generated MEX function.
Define the MATLAB® function integralExpLn.
function out = integralExpLn(xmin, xmax) f = @(x)x.^5.*exp(-x).*sin(x); out = integral(f, xmin, xmax, 'RelTol',1e-10); end
Measure and compare the execution times of integralExpLn and its generated MEX function.
t = coder.perfCompare("integralExpLn",1,{0,Inf})==== Running (integralExpLn, MATLAB) ==== - Running MATLAB script. TimingResult with 1138 Runtime Sample(s) Statistical Overview: mean = 4.40e-04 s max = 2.69e-02 s sd = 8.01e-04 s median = 3.99e-04 s min = 3.45e-04 s 90th = 4.58e-04 s ==== Running (integralExpLn, Codegen Mex) ==== - Generating code and building MEX. - Running MEX. TimingResult with 8785 Runtime Sample(s) Statistical Overview: mean = 5.69e-05 s max = 7.80e-04 s sd = 2.26e-05 s median = 5.33e-05 s min = 4.97e-05 s 90th = 5.62e-05 s
t=1×2 table
MATLAB Codegen Mex
_____________ ______________________________________________________
Runtime (sec) Runtime (sec) Speedup: MATLAB / Codegen Mex (times)
_____________ _____________ _____________________________________
integralExpLn 0.000399 5.3309e-05 7.4847
Compare the execution time of the MATLAB® function linearInterp with the execution times of two MEX functions generated using separate code configuration objects.
Define the MATLAB function linearInterp.
function out = linearInterp(x,v,xq) out = interp1(x,v,xq,"linear"); end
Define input arguments and code generation configurations.
x = 0:pi/1000:2*pi; v = sin(x); xq = 0:pi/2000:2*pi; cfgFastMex = coder.config('mex'); cfgFastMex.IntegrityChecks = false; cfgFastMex.ResponsivenessChecks = false; cfgSIMDMex = coder.config('mex'); cfgSIMDMex.IntegrityChecks = false; cfgSIMDMex.ResponsivenessChecks = false; cfgSIMDMex.SIMDAcceleration = 'Full'; coder.perfCompare("linearInterp",1,{x,v,xq},{cfgFastMex,cfgSIMDMex})
==== Running (linearInterp, MATLAB) ====
- Running MATLAB script.
TimingResult with 4566 Runtime Sample(s)
Statistical Overview:
mean = 1.10e-04 s max = 1.10e-02 s sd = 2.21e-04 s
median = 9.40e-05 s min = 7.20e-05 s 90th = 1.27e-04 s
==== Running (linearInterp, Coder Config 1) ====
- Generating code and building MEX.
- Running MEX.
TimingResult with 7137 Runtime Sample(s)
Statistical Overview:
mean = 7.01e-05 s max = 2.27e-03 s sd = 1.04e-04 s
median = 5.38e-05 s min = 4.95e-05 s 90th = 7.78e-05 s
==== Running (linearInterp, Coder Config 2) ====
- Generating code and building MEX.
- Running MEX.
TimingResult with 2892 Runtime Sample(s)
Statistical Overview:
mean = 1.73e-04 s max = 1.34e-02 s sd = 4.03e-04 s
median = 1.24e-04 s min = 6.07e-05 s 90th = 1.76e-04 s
MATLAB Coder Config 1 Coder Config 2
_____________ _________________________________________________________ _________________________________________________________
Runtime (sec) Runtime (sec) Speedup: MATLAB / Coder Config 1 (times) Runtime (sec) Speedup: MATLAB / Coder Config 2 (times)
_____________ _____________ ________________________________________ _____________ ________________________________________
linearInterp 9.4e-05 5.3819e-05 1.7466 0.000124 0.75806
Compare the execution times of the MEX functions generated from the MATLAB® functions sum1, sum2, and sum3, each with two different code generation configurations.
Define MATLAB® functions sum1, sum2, and sum3.
function out = sum1(in) out = sum(in,2); end
function out = sum2(in) [nOut, nSum] = size(in); out = coder.nullcopy(zeros(nOut,1)); for ii = 1: nOut outLocal = 0; for jj = 1:nSum outLocal = outLocal + in(ii,jj); end end out(ii) = outLocal; end
function out = sum3(in) [nOut, nSum] = size(in); out = zeros(nOut,1); for ii = 1:nSum for jj = 1:nOut out(jj) = out(jj) + in(jj, ii); end end end
Define code generation configurations.
cfg1 = coder.config('mex'); cfg1.IntegrityChecks = false; cfg1.ResponsivenessChecks = false; cfg2 = coder.config('mex'); cfg2.IntegrityChecks = false; cfg2.ResponsivenessChecks = false; cfg2.EnableAutoParallelization = true;
Define inputs, and measure and compare execution times for the six generated MEX functions (two for each sum function).
rng(42);
x = rand(1e4, 1e4);
rSum = coder.perfCompare({'sum1','sum2','sum3'},1,{x},{cfg1,cfg2}, ...
ConfigNames={'Default','Autopar'},CompareWithMATLAB=false)==== Running (sum1, Default) ==== - Generating code and building MEX. - Running MEX. TimingResult with 10 Runtime Sample(s) Statistical Overview: mean = 9.64e-02 s max = 1.01e-01 s sd = 3.06e-03 s median = 9.68e-02 s min = 9.16e-02 s 90th = 9.99e-02 s ==== Running (sum1, Autopar) ==== - Generating code and building MEX. - Running MEX. TimingResult with 10 Runtime Sample(s) Statistical Overview: mean = 9.76e-02 s max = 1.06e-01 s sd = 3.34e-03 s median = 9.66e-02 s min = 9.46e-02 s 90th = 1.02e-01 s ==== Running (sum2, Default) ==== - Generating code and building MEX. - Running MEX. TimingResult with 10 Runtime Sample(s) Statistical Overview: mean = 1.01e-01 s max = 1.06e-01 s sd = 2.64e-03 s median = 1.00e-01 s min = 9.73e-02 s 90th = 1.05e-01 s ==== Running (sum2, Autopar) ==== - Generating code and building MEX. - Running MEX. TimingResult with 10 Runtime Sample(s) Statistical Overview: mean = 1.02e-01 s max = 1.06e-01 s sd = 2.85e-03 s median = 1.03e-01 s min = 9.70e-02 s 90th = 1.05e-01 s ==== Running (sum3, Default) ==== - Generating code and building MEX. - Running MEX. TimingResult with 10 Runtime Sample(s) Statistical Overview: mean = 1.01e-01 s max = 1.06e-01 s sd = 3.79e-03 s median = 1.01e-01 s min = 9.59e-02 s 90th = 1.06e-01 s ==== Running (sum3, Autopar) ==== - Generating code and building MEX. - Running MEX. TimingResult with 10 Runtime Sample(s) Statistical Overview: mean = 9.36e-02 s max = 9.89e-02 s sd = 4.59e-03 s median = 9.42e-02 s min = 8.82e-02 s 90th = 9.89e-02 s
rSum=3×2 table
Default Autopar
_____________ ___________________________________________________
Runtime (sec) Runtime (sec) Speedup: Default / Autopar (times)
_____________ _____________ __________________________________
sum1 0.096819 0.096631 1.0019
sum2 0.099956 0.10277 0.9726
sum3 0.10136 0.094159 1.0764
==== Running (sum1, Default) ====
Perf Compare INFO: Generating code and building MEX.
Perf Compare INFO: Running MEX.
TimingResult with 10 Runtime Sample(s)
Statistical Overview:
mean = 6.53e-02 s max = 6.70e-02 s sd = 1.00e-03 s
median = 6.54e-02 s min = 6.40e-02 s 90th = 6.67e-02 s
==== Running (sum1, Autopar) ====
Perf Compare INFO: Generating code and building MEX.
Perf Compare INFO: Running MEX.
TimingResult with 12 Runtime Sample(s)
Statistical Overview:
mean = 4.46e-02 s max = 4.93e-02 s sd = 2.70e-03 s
median = 4.36e-02 s min = 4.15e-02 s 90th = 4.84e-02 s
==== Running (sum2, Default) ====
Perf Compare INFO: Generating code and building MEX.
Perf Compare INFO: Running MEX.
TimingResult with 10 Runtime Sample(s)
Statistical Overview:
mean = 5.40e-01 s max = 5.56e-01 s sd = 9.38e-03 s
median = 5.40e-01 s min = 5.27e-01 s 90th = 5.53e-01 s
==== Running (sum2, Autopar) ====
Perf Compare INFO: Generating code and building MEX.
Perf Compare INFO: Running MEX.
TimingResult with 10 Runtime Sample(s)
Statistical Overview:
mean = 8.43e-02 s max = 9.70e-02 s sd = 6.77e-03 s
median = 8.40e-02 s min = 7.44e-02 s 90th = 9.49e-02 s
==== Running (sum3, Default) ====
Perf Compare INFO: Generating code and building MEX.
Perf Compare INFO: Running MEX.
TimingResult with 12 Runtime Sample(s)
Statistical Overview:
mean = 4.36e-02 s max = 4.55e-02 s sd = 1.08e-03 s
median = 4.37e-02 s min = 4.14e-02 s 90th = 4.50e-02 s
==== Running (sum3, Autopar) ====
Perf Compare INFO: Generating code and building MEX.
Perf Compare INFO: Running MEX.
TimingResult with 12 Runtime Sample(s)
Statistical Overview:
mean = 4.26e-02 s max = 4.47e-02 s sd = 1.16e-03 s
median = 4.23e-02 s min = 4.09e-02 s 90th = 4.42e-02 s
rSum =
3×2 table
Default Autopar
_____________ ____________________________________
Runtime (sec) Runtime (sec) Speedup wrt Default
_____________ _____________ ___________________
sum1 0.06535 0.043552 1.5005
sum2 0.5395 0.083972 6.4248
sum3 0.043654 0.042331 1.0313
Input Arguments
Names of entry-point functions for code generation and MATLAB execution, specified as a character vector, cell array of character vector
or strings, or string array. When fcnName specifies multiple
entry-point functions, they must all have the same
number of input and output arguments, and the same input argument data types.
Number of outputs the functions return, specified as a nonnegative integer.
Run-time input arguments to fcnName, specified as a cell
array.
Code generation configurations objects. To create these objects, use coder.config.
coder.config("exe") is not supported with
coder.perfCompare.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN, where Name is
the argument name and Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: t =
coder.perfCompare("sum1",1,{x},{cfg1},ConfigNames={"Mex"},CompareWithMATLAB=false);
Names of the output table columns, specified as cell array of character vectors or cell array of strings.
Example: t =
coder.perfCompare({'sum1','sum2'},1,{x},{cfg1,cfg2},ConfigNames={"config1","config2"});
Option to compare execution time of generated code with MATLAB, specified as one of the values in this table.
| Value | Description |
|---|---|
true(default) | Compare the execution time of the MATLAB function with the different versions of the generated code. |
false | Compare the execution times of the different versions of the generated code with each other. |
Example: t =
coder.perfCompare('sum1',1,{x},CompareWithMATLAB=false);
Output Arguments
Execution times in seconds, returned as a table. The table also shows execution time
speedup of each function relative to the first column. If you specify a single
configuration and multiple functions, coder.perfCompare calculates
the speedup relative to the run time of the first function. The table shows the medians
of run times for each entry.
Tips
To achieve consistent results, make sure no other compute-intensive processes are running on the machine where the MATLAB or the generated function is timed.
Version History
Introduced in R2024b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)