InputParser vs exist(...,'var')vs nargin性能

编程入门 行业动态 更新时间:2024-10-23 07:39:57
本文介绍了InputParser vs exist(...,'var')vs nargin性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这三个变体之间在输入检查/默认初始化方面是否有性能比较?

Is there any performance comparison among those three variants for input checking/default initializaion?

将其与最新版本进行比较会很有用,例如R2014b和更旧的R2012b.

It would be useful a comparison on a recent version e.g. R2014b and and older one R2012b.

一个例子:

function foo(a,b) if nargin < 1, a = 1; end if nargin < 2, b = 2; end end versus function foo(a,b) if exist('a','var'), a = 1; end if exist('b','var'), b = 2; end end versus function foo(varargin) p = inputParser; addOptional(p,'a',1) addOptional(p,'b',2) parse(p,varargin{:}) end

在R2014b上使用Amro的测试套件:

Using Amro's testing suite, on R2014b:

func nargs time _________________ _____ __________ 'foo_nargin' 0 2.3674e-05 'foo_exist' 0 3.1339e-05 'foo_inputparser' 0 9.6934e-05 'foo_nargin' 1 2.4437e-05 'foo_exist' 1 3.2157e-05 'foo_inputparser' 1 0.0001307 'foo_nargin' 2 2.3838e-05 'foo_exist' 2 3.0492e-05 'foo_inputparser' 2 0.00015775

推荐答案

下面是一些测试这三种方法的代码:

Here is some code to test the three approaches:

function t = testArgParsing() args = {1, 2}; fcns = { @foo_nargin ; @foo_exist ; @foo_inputparser }; % parameters sweep [f,k] = ndgrid(1:numel(fcns), 0:numel(args)); f = f(:); k = k(:); % test combinations of functions and number of input args t = cell(numel(f), 3); for i=1:size(t,1) t{i,1} = func2str(fcns{f(i)}); t{i,2} = k(i); t{i,3} = timeit(@() feval(fcns{f(i)}, args{1:k(i)}), 2); end % format results in table t = cell2table(t, 'VariableNames',{'func','nargs','time'}); end function [aa,bb] = foo_nargin(a,b) if nargin < 1, a = 1; end if nargin < 2, b = 2; end aa = a; bb = b; end function [aa,bb] = foo_exist(a,b) if ~exist('a','var'), a = 1; end if ~exist('b','var'), b = 2; end aa = a; bb = b; end function [aa,bb] = foo_inputparser(varargin) p = inputParser; addOptional(p,'a',1); addOptional(p,'b',2); parse(p, varargin{:}); aa = p.Results.a; bb = p.Results.b; end

这是我在计算机上获得的R2014a:

Here is what I get in R2014a on my machine:

>> t = testArgParsing t = func nargs time _________________ _____ __________ 'foo_nargin' 0 3.4556e-05 'foo_exist' 0 5.2901e-05 'foo_inputparser' 0 0.00010254 'foo_nargin' 1 2.5531e-05 'foo_exist' 1 3.7105e-05 'foo_inputparser' 1 0.0001263 'foo_nargin' 2 2.4991e-05 'foo_exist' 2 3.6772e-05 'foo_inputparser' 2 0.00015148

还有一个漂亮的图来查看结果:

And a pretty plot to view the results:

tt = unstack(t, 'time', 'func'); names = tt.Properties.VariableNames(2:end); bar(tt{:,2:end}.') set(gca, 'XTick',1:numel(names), 'XTickLabel',names, 'YGrid','on') legend(num2str(tt{:,1}, 'nargin=%d')) ylabel('Time [sec]'), xlabel('Functions')

更多推荐

InputParser vs exist(...,'var')vs nargin性能

本文发布于:2023-10-28 23:27:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:性能   exist   InputParser   nargin   var

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!