필터 지우기
필터 지우기

inputParser found to be very slow in profiler

조회 수: 6 (최근 30일)
David Parks
David Parks 2016년 4월 22일
댓글: Ryan 2022년 10월 25일
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
David Parks
David Parks 2016년 4월 22일
편집: David Parks 2016년 4월 22일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Paul Korswagen
Paul Korswagen 2018년 12월 19일
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
Guillaume
Guillaume 2018년 12월 19일
"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 2022년 10월 25일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by