inputParser found to be very slow in profiler

4 Ansichten (letzte 30 Tage)
David Parks
David Parks am 22 Apr. 2016
Kommentiert: Ryan am 25 Okt. 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 Kommentar
David Parks
David Parks am 22 Apr. 2016
Bearbeitet: David Parks am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Paul Korswagen
Paul Korswagen am 19 Dez. 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 Kommentare
Guillaume
Guillaume am 19 Dez. 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 am 25 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by