有时,检索代码对每个图像的执行略有不同。为了更好地了解您的运行时间,我建议您使用 Matlab 的分析工具。
profile clear;
profile on;
% your code here
profile off;
profile viewer;
使用分析器,您可以查看代码中需要时间的内容。
此外,在您的代码片段中,您调用toc
了两次,这可能会导致您遇到一些奇怪的行为。
对于更具体的措施,您可以尝试
rTimes = zeros(1, numMethods);
% prepare images and what ever you need ...
for imi = 1:numImages
% read test image ...
tic;
% use your method for ret.
rTimes(1) = rTimes(1) + toc; % add this run time to your counter
tic;
% here you run method 2 that you want to compare to...
rTimes(2) = rTimes(2) + toc; % add run time for method 2
% do the same for all methods ...
tic;
% run the numMethods-th method
rTimes(numMethods) = rTimes(numMethods) + toc;
end
% now rTimes hold the running times (overall) for all methods over all test images.