For demonstrating how fast C-compiled functions can be, in one of my courses I use the following function for finding the sum of a list of reals:
myTotal = Compile[{{lst, _Real, 1}},Module[{s=0.}, Do[s=s+z, {z, lst}];s], CompilationTarget->"C"];
In Mathematica 8 and 9, this function is about as fast as the built-in function Total
.
In Mathematica 10.0.0, there was a minor bug in Total
, making Total
three times slower than myTotal
. That bug is repaired now. Even better, while the function myTotal
is as fast as it was, Total
now is almost two times faster:
lst=RandomReal[{0,1}, {2 10^7}];
Do[myTotal[lst], {100}] // RepeatedTiming
Do[Total[lst], {100}] // RepeatedTiming
(* {1.919,Null}
{1.05,Null} *)
RepeatedTiming
works fine. However, in Mathematica 10.3, Timing
for Total
does not work well:
Do[myTotal[lst], {100}] // Timing
Do[Total[lst], {100}] // Timing
(* {1.93441,Null}
{0.,Null} *)
Do[myTotal[lst], {100}] // AbsoluteTiming
Do[Total[lst], {100}] // AbsoluteTiming
(* {1.92955,Null}
{1.05932,Null} *)
Do[Total[lst], {1000}] // Timing
(* {0.0312002,Null} *)
This looks like a minor bug to me. Is this bug restricted to Windows, or is it a 'general' bug?
Total
. For example,Do[Exp[lst];, {50}] // Timing
returns{0., Null}
, whereasDo[Exp[lst];, {50}] // AbsoluteTiming
returns{2.14685, Null}
on my PC with Win 10 and Mma 10.3. $\endgroup$Timing
, as I consider it meaningless. Or as the documentation puts it: "it may ..." and "on some operating systems ...". $\endgroup$Timing
was originally meant to measure total CPU time in the main kernel process. It is far from meaningless in principle, and if it worked reliably, it would probably be more broadly useful thanAbsoluteTiming
. But, I agree that more effort needs to be spent to make its operation reliable and consistent enough to trust it. $\endgroup$Timing
behaves as documented. It's just not necessarily the timing function one would like to have. $\endgroup$