23
$\begingroup$

Issue introduced in version 10, fixed in 10.0.2


Compilation nowadays may give very fast functions. Here is an example for finding the sum of a list of machine numbers, just as the built-in function Total:

myTotal =   Compile[{{lst, _Real, 1}}, 
  Module[{s = 0.}, Do[s = s + x, {x, lst}]; s], 
  CompilationTarget -> "C"];

lst = RandomReal[{0, 1}, {10^7}];
myTotal[lst] == Total[lst]
(*True *)

In Mathematica 10, I noted the surprising fact that the function myTotal is more than three times as fast as Total:

Do[myTotal[lst], {500}] // Timing (* {4.758031, Null} *)
Do[Total[lst], {500}] // Timing (* {16.052503, Null} *)

In Mathematica 9, they are as fast. So in Mathematica 10 (on Windows), Total is more than 3 times slower than in Mathematica 9, fortunately of course still very fast. What could be the reason for this slowdown?

$\endgroup$
9
  • 12
    $\begingroup$ It's a known issue, reported and under investigation. $\endgroup$ Oct 15, 2014 at 15:55
  • 3
    $\begingroup$ @DanielLichtblau Is it at this point decided whether there'll be a 10.0.2? Or was 10.0.1 the last 10.0.x version? $\endgroup$
    – Szabolcs
    Oct 15, 2014 at 16:05
  • 9
    $\begingroup$ There are so many slow-downs in M10 that there have to me more bug-fix releases. I just go on mostly with V9 for the time being. Groetjes, Rolf $\endgroup$ Oct 15, 2014 at 16:11
  • 5
    $\begingroup$ @Szabolcs I believe there will be a 10.0.2 but my beliefs sometimes depart from the real world. $\endgroup$ Oct 15, 2014 at 18:02
  • 5
    $\begingroup$ Just a note, Total is more precise then the direct summation. $\endgroup$
    – ybeltukov
    Oct 15, 2014 at 19:07

1 Answer 1

11
$\begingroup$

Until this issue gets fixed by WRI you can use Tr instead of Total

Do[myTotal[lst], {500}] // Timing
Do[Total[lst], {500}] // Timing 
Do[Tr[lst], {500}] // Timing 
{5.241634, Null}  
{16.161704, Null}  
{5.194833, Null}
myTotal[lst] == Total[lst] == Tr[lst]
True
$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.