Function to avoid evaluation of assert condition
Version 1.0.1 (3.83 KB) by Dave Ober
After debugging code, this function enables developers to turn off the evaluation of assert conditions (if desired) to save CPU cycles.
Updated 31 May 2022
Have you ever wanted to turn off the evaluation of "expensive" assert conditions after you have completed debugging your code to save CPU Cycles? Well then assertF is the function for you.
It has minimal overhead and supports the same inputs (errID, msg, A1,...,An) as MATLAB's assert function. It utilizes a "persistent" assertFlag variable that allows developers to set the flag (at the start of code execution) and as long as the assert condition calls include a function handle "@()", then the condition is not evaluted if the assertFlag was set to false. However, if a developer wants the condition to always be evaluated inpendent of the assertFlag (like the MATLAB assert function), then exclude the function handle from the condition (or always include the assertFlag as the 2nd variable - although this adds a touch more overhead)
There are four files that help demonstrate how to effectively use this function.
assertF.m: The assert function that enables an assertFlag to determine if conditions with function handles are evaluated.
Test_AssertCallTimes.m: Example file that demonstrates how expensive condition calls using assertF compare with MATLAB's assert function and how much time is saved when the assertFlag is set to false for the same conditions. This file also shows how to use assertF with and without function handles. To get accurate evaluation times we use MATLAB's timeit function.
Test_AssertFalseCases.m: Example file that has six different condition cases that are all false (and throw errors) and are caught in a try/catch block.
AssertFalseCase.m: The six cases called by Test_AssertFalseCases.m
Dave Ober (2023). Function to avoid evaluation of assert condition (https://www.mathworks.com/matlabcentral/fileexchange/111865-function-to-avoid-evaluation-of-assert-condition), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Created with R2022a
Compatible with R2008a and later releases
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.