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. Tagging it as bug. – Szabolcs Nov 02 '15 at 09:12Total. For example,Do[Exp[lst];, {50}] // Timingreturns{0., Null}, whereasDo[Exp[lst];, {50}] // AbsoluteTimingreturns{2.14685, Null}on my PC with Win 10 and Mma 10.3. – Karsten7 Nov 02 '15 at 11:34Timing, as I consider it meaningless. Or as the documentation puts it: "it may ..." and "on some operating systems ...". – Karsten7 Nov 02 '15 at 11:45Timingwas 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. – Oleksandr R. Nov 02 '15 at 11:59Do[Total[lst], {1000}]; // Timing // AbsoluteTimingreturns{10.0118, {0.046875, Null}}. Probably most of the calculation is just done in a way that "on some operating systems,Timingmay ignore". – Karsten7 Nov 02 '15 at 16:20Totalis more intelligent then the direct sum. – ybeltukov Nov 02 '15 at 18:56Timingbehaves as documented. It's just not necessarily the timing function one would like to have. – Karsten7 Nov 02 '15 at 19:40Timing, though. In the past the problem wasn't obvious because few functions were multithreaded. Now, as the rest of Mathematica improves,Timingis becoming increasingly useless. – Oleksandr R. Nov 02 '15 at 22:55