Skip to content

Instantly share code, notes, and snippets.

@amroamroamro
Last active August 29, 2015 14:08
Show Gist options
  • Save amroamroamro/df8a6c3e79062639763f to your computer and use it in GitHub Desktop.
Save amroamroamro/df8a6c3e79062639763f to your computer and use it in GitHub Desktop.
[MATLAB] benchmark for parsing cell-array of strings
function t = bench_runner(fcns, p_rows, p_cols)
out = cell(0,4);
fnames = cellfun(@func2str, fcns, 'Uniform',false);
% parameters sweep
[nrows,ncols] = ndgrid(p_rows, p_cols);
nrows = nrows(:); ncols = ncols(:);
for k=1:numel(nrows)
% generate data of specified size
fprintf('=== %dx%d ===\n', nrows(k), ncols(k));
words = rand_cellstr(nrows(k), ncols(k));
% check results are equal
fprintf('Checking output... ')
v = cellfun(@(f) feval(f,words), fcns, 'UniformOutput',false);
if ~isequal(v{:})
fprintf('FAIL\n')
continue
else
fprintf('PASS\n')
end
clear v
% time functions, and store results
for f=1:numel(fcns)
fprintf('%40s: ', fnames{f});
if ~isempty(strfind(fnames{f}, '_gpu'))
tsec = gputimeit(@() feval(fcns{f}, words), 1);
else
tsec = timeit(@() feval(fcns{f}, words), 1);
end
fprintf('%f\n', tsec);
out(end+1,:) = {fnames{f}, nrows(k), ncols(k), tsec}; %#ok<AGROW>
end
end
% plot results, and return them as a table
t = bench_plot_results(out);
end
function C = rand_cellstr(M,N)
frmt = [repmat('%d_',1,N-1) '%d'];
C = strrep(cellstr(num2str(randi([0 9999], [M N]), frmt)),' ','');
end
%{
% a lighter version of TIMEIT if you don't want to waste hours :)
function t = timeit(f, nout)
if nargin < 2, nout = 1; end
out = cell(1,nout);
N = 5; % number of repetitions
t = zeros(N,1);
f();f();f(); % warm up function
for i=1:N
tocId = tic;
[out{:}] = feval(f);
t(i) = toc(tocId);
end
t = mean(t);
end
%}
function t = bench_plot_results(out)
%% pivot table
t = cell2table(out, 'VariableNames',{'func', 'nrows', 'ncols', 'time'});
tt = unstack(t, 'time', 'func');
% prepare data for plotting
y = tt{:,3:end}.';
fnames = tt.Properties.VariableNames(3:end);
x = 1:numel(fnames);
str = strtrim(cellstr(num2str(tt{:,1:2},'%dx%d')));
% sort by times
[~,ord] = sortrows(y, -(size(y,2):-1:1));
y = y(ord,:);
fnames = fnames(ord);
%% plot
pos = get(0,'DefaultFigurePosition');
figure(1), set(gcf, 'Position',pos.*[1 1 1.2 1.75]), movegui(gcf,'east')
plot(y, x, 'Marker','.', 'MarkerSize',20, 'LineWidth',2)
set(gca, 'YTick',x, 'YTickLabel',fnames, 'YLim',[0 x(end)+1], ...
'XGrid','on', 'YGrid','on', 'XScale','log')
xlabel('Time [sec]'), ylabel('Functions')
legend(str, 'Orientation','horiz', 'Location','NorthOutside')
figure(2), set(gcf, 'Position',pos.*[1 1 1.2 1.75]), movegui(gcf,'west')
barh(x, y, 1.0, 'EdgeColor','k')
set(gca, 'YTick',x, 'YTickLabel',fnames, 'YLim',[0 x(end)+1], ...
'XGrid','on', 'XScale','log', 'YAxisLocation','right')
xlabel('Time [sec]'), ylabel('Functions')
legend(str, 'Orientation','horiz', 'Location','NorthOutside')
end
% get list of available functions
% (I'm assuming necessary folders are already added to MATLAB path)
d = dir(fullfile('bench_functions','*.m'));
[~,f] = cellfun(@fileparts, {d(~[d.isdir]).name}, 'Uniform',false);
% filter out ones known to be slow!
f(strncmpi(f, 'func_str2double_', numel('func_str2double_'))) = [];
% function handles
fcns = cellfun(@str2func, f, 'Uniform',false)';
% run benchmarks and show report
t = bench_runner(fcns, 10.^(1:5), 10.^(1:1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment