使用tic toc进行基准测试 [英] Using tic toc for benchmark MATLAB
问题描述
昨天我正在测试是否使用for循环添加元素和数组比使用内置的MATLAB函数总和(据我了解这应该是这样的,因为内置的函数是预编译的),但是我得到了一些奇怪的结果:
r = rand(1e8,1);
tic
sum1 = 0;
for i = 1:1e8
sum1 = sum1 + r(i);
结束
t1 = toc;
tic
sum2 = sum(r);
t2 = toc;
>> t1
t1 =
0.5872
>> t2
t2 =
0.1053
给我那些结果(MATLAB 2011)。不过我在MATLAB 2013中测试过,总和比for循环差。我不知道我是搞砸了还是错过了什么?
哪个更好? for循环或总和?
function timing_builtins()
%Setup
rng(123);
r = rand(1e8,1);
函数test1(r)
sum1 = 0;
for i = 1:1e8
sum1 = sum1 + r(i);
end
end
function test2(r)
sum2 = sum(r);
end
t1 = timeit(@()test1(r));
t2 = timeit(@()test2(r));
格式长g;
fprintf('For循环:%f秒\',t1);
fprintf('Sum call:%f seconds\\\
',t2);
end
这应该会给你一个更好的加速视图,大概是10倍对于 sum
在上覆盖在这个特定情况下。
如果我们调用预定义变量 r
,我们可以看到内置的 sum
确实比用于
循环,因子为4
内置函数几乎总是更快的选择。 BLAST引擎最近已经大幅度降低了这个因素(我使用了2012a),但是对于完全相同的操作,内置通常会更快。这同样适用于使用 bsxfun
向量化的东西,这也会(几乎)总是比使用循环更快。
引用@beaker:
更一般的问题的答案是这取决于。有时循环会更快,因为它们不会执行广义内置的所有错误检查和类型转换。
感谢@Daniel和@rayryeng调用 sum
确实是在最新的MATLAB版本中更快。原因是 sum
几乎不需要检查,因为求和元素中没有太多的错误。另外, sum
native从LAPACK / SuiteSparse接口调用一个函数,该函数高度优化。
yesterday I was testing whether using a for loop for adding up elements in and array was worse than using the built-in MATLAB function sum (As far I understand this should be the case, since built-in functions are pre-compiled), however I got some weird results:
r = rand(1e8, 1);
tic
sum1 = 0;
for i = 1:1e8
sum1 = sum1 + r(i);
end
t1 = toc;
tic
sum2 = sum(r);
t2 = toc;
>> t1
t1 =
0.5872
>> t2
t2 =
0.1053
it gives me those results (MATLAB 2011). However I tested it in MATLAB 2013, and using sum was worse than the for loop. I don't know whether I messed up or I missed something?
Which is better? for loop or sum?
function timing_builtins()
% Setup
rng(123);
r = rand(1e8, 1);
function test1(r)
sum1 = 0;
for i = 1:1e8
sum1 = sum1 + r(i);
end
end
function test2(r)
sum2 = sum(r);
end
t1 = timeit(@() test1(r));
t2 = timeit(@() test2(r));
format long g;
fprintf('For loop: %f seconds\n', t1);
fprintf('Sum call: %f seconds\n', t2);
end
This should give you a better view of the speedup, it is about 10 times for sum
over the for
in this specific case.
If we call the predefined variable r
instead, we can see that the built-in sum
is indeed faster than the for
loop, with a factor of 4.
Built-ins are almost always the faster choice. The BLAST engine has been overhauled recently decreasing this factor (I used 2012a), but for the exact same operation a built-in will usually be faster. The same holds for vectorising things using bsxfun
, that'll also be (almost) always faster than using loops.
The answer to the more general question is, "it depends." Sometimes loops are faster because they don't do all of the error checking and type conversions that a generalized built-in would.
Thanks to @Daniel and @rayryeng it seems the call to sum
is indeed faster still in the newest MATLAB versions. The reason for this is that sum
requires almost no checks, since there is not much that can go wrong in summing elements. Also the sum
native calls a function from the LAPACK / SuiteSparse interface, which is highly optimised.
这篇关于使用tic toc进行基准测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!