Last active
June 30, 2016 21:17
-
-
Save kouichi-c-nakamura/efbc4973062b1cffb7d601f026215a0b to your computer and use it in GitHub Desktop.
A reason why you should avoid MATLAB-builtin waitbar() and instead use fprintf() ref: http://qiita.com/kouichi-c-nakamura/items/ae9dc371eade0fd0a945
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% waitbarVAfprintf | |
trials = 10; | |
elapsed1 = zeros(10,1); | |
for j = 1:10 | |
clear n | |
tic | |
n = 1000; | |
fprintf('%d to go\n',n) | |
for i = 1:n | |
fprintf('*') | |
end | |
fprintf('\n') | |
elapsed1(j) = toc; | |
end | |
elapsed2 = zeros(10,1); | |
for j = 1:10 | |
clear n | |
tic | |
n = 1000; | |
for i = 1:n | |
if round(i/n*100) > round((i-1)/n*100) | |
fprintf('*') | |
end | |
end | |
fprintf('\n') | |
elapsed2(j) = toc; | |
end | |
elapsed3 = zeros(10,1); | |
for j = 1:10 | |
clear n wbmsg wb | |
tic | |
n = 1000; | |
for i = 1:n | |
wbmsg = sprintf('In progress: %.1f%%', i/n*100); | |
if ~exist('wb', 'var') | |
wb = waitbar((i-1)/n, wbmsg); | |
else | |
waitbar((i-1)/n, wb, wbmsg); | |
end | |
end | |
delete(wb) % cannot use 'close'!!! | |
elapsed3(j) = toc; | |
end | |
elapsed4 = zeros(10,1); | |
for j = 1:10 | |
clear n wbmsg wb | |
tic | |
n = 1000; | |
for i = 1:n | |
if round(i/n*100) > round((i-1)/n*100) | |
wbmsg = sprintf('In progress: %.1f%%', i/n*100); | |
if ~exist('wb', 'var') | |
wb = waitbar((i-1)/n, wbmsg); | |
else | |
waitbar((i-1)/n, wb, wbmsg); | |
end | |
end | |
end | |
delete(wb) % cannot use 'close'!!! | |
elapsed4(j) = toc; | |
end | |
%% | |
fprintf('1. fprintf: %f %s %f msec per cycle\n',... | |
mean(elapsed1),char(177),std(elapsed1)); | |
fprintf('2. fprintf for each %%: %f %s %f msec per cycle\n',... | |
mean(elapsed2),char(177),std(elapsed2)); | |
fprintf('3. waitbar: %f %s %f msec per cycle\n',... | |
mean(elapsed3),char(177),std(elapsed3)); | |
fprintf('4. waitbar for each %%: %f %s %f msec per cycle\n',... | |
mean(elapsed4),char(177),std(elapsed4)); | |
fprintf('fprintf is %.1f-fold faster than waitbar updating at every cycle\n',... | |
mean(elapsed3./elapsed1)); | |
fprintf('fprintf for each %% is %.1f-fold faster than waitbar for each %%\n',... | |
mean(elapsed4./elapsed2)); | |
fprintf('fprintf for each %% is %.1f-fold faster than frpintf updating at every cycle\n',... | |
mean(elapsed1./elapsed2)); | |
fprintf('waitbar for each %% is %.1f-fold faster than waitbar updating at every cycle\n',... | |
mean(elapsed3./elapsed4)); | |
fprintf('fprintf for each %% is %.1f-fold faster than waitbar updating at every cycle\n',... | |
mean(elapsed3./elapsed2)); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fprintf is 360.9-fold faster than waitbar updating at every cycle | |
fprintf for each % is 510.3-fold faster than waitbar for each % | |
fprintf for each % is 10.7-fold faster than frpintf updating at every cycle | |
waitbar for each % is 7.5-fold faster than waitbar updating at every cycle | |
fprintf for each % is 3829.7-fold faster than waitbar updating at every cycle | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 1000; | |
for i = 1:n | |
if round(i/n*100) > round((i-1)/n*100) | |
wbmsg = sprintf('In progress: %.1f%%', i/n*100); | |
if ~exist('wb', 'var') | |
wb = waitbar((i-1)/n, wbmsg); | |
else | |
waitbar((i-1)/n, wb, wbmsg); | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment