23

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?

dr.blochwave
  • 8,768
  • 3
  • 42
  • 76
Fred Simons
  • 10,181
  • 18
  • 49
  • 12
    It's a known issue, reported and under investigation. – Daniel Lichtblau Oct 15 '14 at 15:55
  • 3
    @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? – Szabolcs Oct 15 '14 at 16:05
  • 9
    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 – Rolf Mertig Oct 15 '14 at 16:11
  • Just a comment that if you're trying, in effect, to average the summation over 10^7 random reals, would it not make more sense to use ls := RandomReal[{0, 1}, {10^2}], that is, with a SetDelayed? – murray Oct 15 '14 at 16:38
  • 1
    @Murray. I wanted to compare 500 evaluations of both myTotal and Total on the same list. With SetDelayed, I would have got 1000 different lists, and moreover the time needed for constructing these lists would be included in the Timings. – Fred Simons Oct 15 '14 at 16:50
  • 5
    @Szabolcs I believe there will be a 10.0.2 but my beliefs sometimes depart from the real world. – Daniel Lichtblau Oct 15 '14 at 18:02
  • 5
  • @Fred Simons: Understood why you wanted to use very same list for all 500 evaluations. I suggested varying the list just to eliminate anything special about the particular list of random numbers you might generate. – murray Oct 15 '14 at 20:51
  • 1
    I tested that in v.10.0.2 (OS X 10.9.5) : Total is now as fast as myTotal. – SquareOne Dec 19 '14 at 18:43

1 Answers1

11

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
Karsten7
  • 27,448
  • 5
  • 73
  • 134