performance: fullfile function execution efficiency could be improved.

2 views (last 30 days)
The fullfile execution efficiency could be improved with the following tests:
t1 = tic;
for i = 1:10000
f = fullfile('c:\','myfiles','matlab',filesep);
end
elapsedTime1 = toc(t1)
elapsedTime1 = 0.3777
t2 = tic;
for i = 1:10000
f = "c:/"+"myfiles/"+"matlab"+filesep;
end
elapsedTime2 = toc(t2)
elapsedTime2 = 0.0163
elapsedTime1/elapsedTime2
ans = 23.1725

Answers (1)

Jan
Jan on 11 Jul 2022
Seriously?
fullfile inserts file separators on demand, while your code is a simple concatenation of strings. In addition fullfile considers CHAR vectors, strings. cell strings and string arrays also. In your example you have to insert a file separator manually:
f = "c:/"+"myfiles/"+"matlab"+filesep;
% ^
But, yes, it can be improved. See FEX: JoinPath
t1 = tic;
for i = 1:10000
f = fullfile('c:\','myfiles');
end
elapsedTime1 = toc(t1)
elapsedTime1 = 0.1240
t2 = tic;
for i = 1:10000
f = JoinPath('c:\','myfiles');
end
elapsedTime2 = toc(t2)
elapsedTime2 = 0.0723
function F = JoinPath(Arg1, Arg2, varargin)
% Join parts of path and file name
% FullName = JoinPath(string1, string2, ...)
% The input strings are joined to a file path. Leading and/or trailing file
% separators are considered. This works exactly as FULLFILE, but is is
% faster.
%
% See also FULLFILE, FILEPARTS, SplitFile, TrailFSep, GetFileName, ParentPath.
%
% Tested: Matlab 6.5, 7.7, 7.8, 7.13, 8.6, WinXP/32, Win7/64
% Author: Jan Simon, Heidelberg, (C) 2009-2016 matlab.2010(a)n(MINUS)simon.de
% $JRev: R-k V:028 Sum:cbAk1P0AGf1V Date:18-Feb-2016 23:42:43 $
% $License: BSD (use/copy/change/redistribute on own risk, mention the author) $
% $UnitTest: uTest_JoinPath $
% $File: Tools\GLFile\JoinPath.m $
% History:
% 028: 18-Feb-2016 23:37, Published in FileExchange.
% Replace file separators from \ to / or vice versa.
% Get file separators:
persistent FSep wuFSep
if isempty(FSep)
FSep = filesep;
if FSep == '\'
wuFSep = '/';
else
wuFSep = '\';
end
end
% Join the parts:
nArg = nargin;
if nArg >= 2
Arg1 = strrep(Arg1, wuFSep, FSep);
Arg2 = strrep(Arg2, wuFSep, FSep);
lArg1 = length(Arg1); % Optimized for most frequent task - 2 arguments:
lArg2 = length(Arg2);
if lArg1 * lArg2 ~= 0 % faster than: ~isempty(Arg1) && ~isempty(Arg2)
if Arg1(lArg1) ~= FSep
if Arg2(1) ~= FSep
F = [Arg1, FSep, Arg2];
else % Arg2 starts with separator
F = [Arg1, Arg2];
end
else % Arg1 end with separator
if Arg2(1) ~= FSep
F = [Arg1, Arg2];
else % Arg2 starts with separator
Arg1(lArg1) = [];
F = [Arg1, Arg2];
end
end
else
F = [Arg1, Arg2];
end
% Append arguments from 3 to end:
if nArg > 2
ArgC = strrep(varargin, wuFSep, FSep);
for iC = 1:length(varargin)
P = ArgC{iC};
lF = length(F);
lP = length(P);
if lF * lP ~= 0 % same as: ~isempty(F) & ~isempty(P)
if F(lF) ~= FSep
if P(1) ~= FSep
F = [F, FSep, P]; %#ok<AGROW>
else % P starts with separator
F = [F, P]; %#ok<AGROW>
end
else % F ends with separator
if P(1) == FSep % Consider leading separator of P:
F(lF) = [];
end
F = [F, P]; %#ok<AGROW>
end
else % Either F or P is empty:
F = [F, P]; %#ok<AGROW>
end
end % for i
end % if nArg > 2
elseif nArg == 1
F = strrep(Arg1, wuFSep, FSep);
elseif nArg == 0
F = '';
end
end

Categories

Find more on File Name Construction in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!