inputParser found to be very slow in profiler

5 views (last 30 days)
David Parks
David Parks on 22 Apr 2016
Commented: Ryan on 25 Oct 2022
I'm using inputParser. In a profiling session I discovered something disturbing, inputParser is costing more time than even large matrix operations (where cpu time should be).
Just the setup of inputPaser cost 0.5s on 3000 function calls.
p = inputParser
To give a scale comparison, running bsxfun to add arrays to medium size matrices later in the same function took 0.36s on 6000 calls. In what world can instantiating the inputParser be more than twice as costly as a non-trivial matrix operation?
Worse by far is parse:
parse( p, required_struct_with_large_matrices, required_large_matrix, varargin(:) )
2.33s on 3000 calls to parse. That was more time-expensive than a medium/large matrix-matrix multiplication later in the code. Yikes!
I don't see any threads discussing this elsewhere. Anyone have an idea what might be going on here? Is inputParser really the disaster it appears to be? Or are there quirks I don't know about yet?
If I remove the 2 required variables, the struct and large matrix, and just process 1 single optional parameter (a string with 2 possible options with a validation of `@(v)any(strcmp(PREDICT_MODE,v))`) the time cost drops from 2.33s down to 1s. A big improvement, but still completely horrible, and more costly than non-trivial matrix operations later in the code.
  1 Comment
David Parks
David Parks on 22 Apr 2016
Edited: David Parks on 22 Apr 2016
Here's a trivial copy/paste example that demonstrates the problem, it performs each step 3000 times. This just parses 2 trivial strings. Adding large matrices exacerbates the problem, but even with trivial inputs like this it becomes obvious.
% Setup
STR_COMPARE = {'option A', 'option B'};
iters = 3000;
% inputParser initializations, kinda slower than it should be
tic; for i = 1:iters; p = inputParser; end; toc;
% Add a couple simple optional string parameters, untimed
addOptional(p,'option1','option B',@(v)any(strcmp(STR_COMPARE,v)));
addOptional(p,'option2','option B',@(v)any(strcmp(STR_COMPARE,v)));
% Parsing 2 string parameters should be trivial
tic;
for i = 1:iters;
parse( p, 'option1', 'option A', 'option2', 'option B' );
end;
toc;
% Comparable non-trivial operation.
tic; for i = 1:iters; rand(120,120); end; toc;
% Output (initialize / parse / compare to rand(120:120))
% Elapsed time is 0.047984 seconds.
% Elapsed time is 0.403222 seconds.
% Elapsed time is 0.360593 seconds.

Sign in to comment.

Answers (1)

Paul Korswagen
Paul Korswagen on 19 Dec 2018
You should not run the large matrices through inputParser.
function example(largematrix,varargin)
a = inputParser;
%Required:
addRequired(a,'LargeMatrix');
%Parameter:
addParameter(a,'NV1',0);
parse(a,1,varargin{:}); %note the placeholder for the LargeMatrix
end
  2 Comments
Guillaume
Guillaume on 19 Dec 2018
"You should not run the large matrices through inputParser".
Have you got any explanation to support that statement? Passing a matrix, regardless of its size to a function should be instantaneous as long as the matrix is not modified. It's just one shared pointer copy.
Ryan
Ryan on 25 Oct 2022
I ran into this same issue. When I used placeholders like stated above, the time went from ~53 seconds to ~0.0003 seconds. I'm not sure why this happens though.

Sign in to comment.

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!