coder.timeit - Measure execution time of generated C/C++ code - MATLAB (original) (raw)
Measure execution time of generated C/C++ code
Since R2024b
Syntax
Description
[[t](#mw%5F468a955d-b41c-4a36-8822-c3836b545184),[trObj](#mw%5F6aa225be-11b7-474f-a921-2ad96ace8c22),[benchObj](#mw%5F8196a2a0-220f-4b5f-91a7-b01cc94da59e)] = coder.timeit([fcnName](#mw%5F7eff3904-c843-40c5-a120-5c445eeffc1b),[numOutputs](#mw%5F74460277-bcd7-48c2-b77d-bfd29d89b67a),[runtimeArgs](#mw%5Fda97796b-1ec3-4216-af34-fa4fc85f7f75))
measures the execution time of the code generated from the MATLAB® function fcnName
. The generated code is executed with input arguments runtimeArgs
and returns numOutputs
output arguments.
- By default,
coder.timeit
uses a MEX configuration object withIntegrityChecks
andResponsivenessChecks
properties set tofalse
. coder.timeit
measures the execution times for multiple runs and returns the median execution time.- The
coder.timeit
function uses internal heuristics to determine the number of runs. coder.timeit
excludes the timing overhead incurred by the data transfer between MATLAB and generated code execution.
[~,~,[benchObj](#mw%5F8196a2a0-220f-4b5f-91a7-b01cc94da59e)] = coder.timeit([fcnName](#mw%5F7eff3904-c843-40c5-a120-5c445eeffc1b),[numOutputs](#mw%5F74460277-bcd7-48c2-b77d-bfd29d89b67a),[runtimeArgs](#mw%5Fda97796b-1ec3-4216-af34-fa4fc85f7f75),"generatecodeonly")
creates a coder.performance.Benchmarkable object that can be used to timefcnName
multiple times with different inputs without having to regenerate code every time. The function packages the generated code inside a wrapper object benchObj
.
[[t](#mw%5F468a955d-b41c-4a36-8822-c3836b545184),[trObj](#mw%5F6aa225be-11b7-474f-a921-2ad96ace8c22),[benchObj](#mw%5F8196a2a0-220f-4b5f-91a7-b01cc94da59e)] = coder.timeit(___,[Name=Value](#namevaluepairarguments))
specifies additional options using one or more name-value arguments.
Examples
Measure the execution time of the generated C code for the MATLAB® function linearInterp
.
Define the MATLAB® function linearInterp
.
function out = linearInterp(x,v,xq) out = interp1(x,v,xq,"linear"); end
Measure the execution time of linearInterp
by using the coder.timeit
function.
x = 0:pi/1000:2pi; v = sin(x); xq = 0:pi/10000:2pi;
t = coder.timeit("linearInterp",1,{x,v,xq})
- Generating code and building MEX.
- Running MEX. TimingResult with 4050 Runtime Sample(s)
Statistical Overview: mean = 1.23e-04 s max = 2.27e-03 s sd = 1.29e-04 s median = 9.28e-05 s min = 8.39e-05 s 90th = 1.52e-04 s
Generate code without executing the timing function and times the function for multiple sets of inputs using the generated code.
Define the MATLAB® function filterSignal
:
function out = filterSignal(a,b,x) out = filter(a,b,x); end
Define compile-time input arguments and generate code with the coder.timeit
function using the "generatecodeonly
" option. The coder.timeit
function creates a wrapper object that you can use to measure the execution time of the generated code with different input arguments.
a = [1 1]; b = [2 2]; x = 1:1e7;
[,,bObj] = coder.timeit("filterSignal",1,{a,b,x},"generatecodeonly", ...
CompileArgs={a,b,coder.typeof(x,[1 Inf])})
- Generating code and building MEX.
bObj = Benchmarkable with properties:
CompileArguments: {[1 1] [2 2] [1×1 coder.PrimitiveType]}
CoderConfig: [1×1 coder.MexCodeConfig]
Name: 'filterSignalTestWrapper'
Measure the execution time of the generated code for different input values.
- Running MEX. TimingResult with 4985 Runtime Sample(s)
Statistical Overview: mean = 1.00e-04 s max = 5.96e-04 s sd = 1.43e-05 s median = 9.85e-05 s min = 9.73e-05 s 90th = 1.00e-04 s
- Running MEX. TimingResult with 10 Runtime Sample(s)
Statistical Overview: mean = 1.96e-01 s max = 2.16e-01 s sd = 1.99e-02 s median = 1.99e-01 s min = 1.43e-01 s 90th = 2.13e-01 s
t = table([1e4; 1e7],[t1; t2],'VariableNames',{'Size','Runtime (s)'})
t=2×2 table Size Runtime (s) _____ ___________
10000 9.8522e-05
1e+07 0.19888
Measure the execution time of the generated code for the linearInterp
function as a standalone C static library.
Define the MATLAB® function linearInterp
.
function out = linearInterp(x,v,xq) out = interp1(x,v,xq,"linear"); end
Measure the execution time of linearInterp
. Specify the code configuration, working directory, and code generation options.
x = 0:pi/100:2pi; v = sin(x); xq = 0:pi/400:2pi; cfg = coder.config("lib"); [t,trObj] = coder.timeit("linearInterp",1,{x,v,xq},CoderConfig=cfg, ... WorkDirectory='RowMajor_folder',codegenCmdArgs={'-rowmajor'})
- Generating code and building SIL/PIL MEX.
- Running MEX. TimingResult with 24084 Runtime Sample(s)
Statistical Overview: mean = 2.08e-05 s max = 2.44e-03 s sd = 7.57e-05 s median = 5.64e-06 s min = 4.87e-06 s 90th = 1.33e-05 s
trObj = TimingResult with 24084 Runtime Sample(s)
Statistical Overview: mean = 2.08e-05 s max = 2.44e-03 s sd = 7.57e-05 s median = 5.64e-06 s min = 4.87e-06 s 90th = 1.33e-05 s
Input Arguments
Name of entry-point function, specified as a character vector or string scalar.
Number of outputs the function returns, specified as a nonnegative integer.
Run-time input arguments to fcnName, specified as a cell array.
Name-Value Arguments
Specify optional pairs of arguments asName1=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.timeit("testPerf",1,{1:1000},CompileArgs={coder.typeof(0,[1 Inf])},MinRuns=10,MinTime=1,CoderConfig={mcfg},WorkDirectory=fullfile(pwd,'newworkdir'),CodegenCmdArgs={'-rowmajor','-g'})
Compile time input arguments to MATLABfcnName, specified as a cell array.
Use these input arguments to specify input types for code generation.
Example: t = coder.timeit("testPerf",1,{1:1000},CompileArgs={coder.typeof(0,[1 Inf])})
Minimum number of trials to ensure runtime stability, specified as a nonnegative integer. The default value ofMinRuns
is:
- 5 — When execution time is greater than or equal to 30 seconds
- 10 — When execution time is less then 30 seconds
Example: t = coder.timeit("testPerf",1,{1:1000},MinRuns=7)
Minimum time duration in seconds, specified as a nonnegativedouble
.
Example: t = coder.timeit("testPerf",1,{1:1000},MinTime=1)
Code generation configurations object. To create the object, use coder.config.
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.
Example: t = coder.timeit("testPerf",1,{1:1000},CoderConfig=cfg)
Work directory path for generating code, specified as a character vector.
Example: coder.timeit("testPerf",1,{1:1000},WorkDirectory= fullfile(pwd,'newworkdir'))
Code generation arguments, specified as cell array of character vectors or cell array of string scalars.
For more information on code generation options, see codegen.
Example: coder.timeit("testPerf",1,{1:1000},CodegenCmdArgs={'-rowmajor', '-g'})
Output Arguments
Median of all run time measurements in seconds, specified as double
scalar.
Input and output information for benchmarking of the generated code, specified as coder.performance.Benchmarkable object. Use this object to reruncoder.timeit
without regenerating code and evaluate performance across multiple input sets.
Tips
- To achieve consistent results, make sure no other computation-intensive processes are running on the machine where you measure the execution time of the MATLAB function or the generated code.
- Use the name-value arguments
MinRuns
andMinTime
to can control the number of times to run the generated code. - The code generator takes longer to measure the execution time of code generated with
lib
ordll
build types than with themex
build type. - When using multiple instances of MATLAB and running
coder.timeit
from the same folder, use theWorkDirectory
name-value argument to specify different folders.
Version History
Introduced in R2024b