I have been using Mathematica for some time now but I have a feeling that I am not using it as efficiently as it could be.

I have the following problem which I am unable to figure out and it would be great if someone could point me in the right direction:

I have a function which is of the following form:

f[x_]:= Total[A1/(A2+x),Infinity]; 

where A1 and A2 are very large arrays (2500x2500 real numbers) (Sorry for the way I wrote the function but I haven't figured out how to insert code into the question (not very good at this type of typesetting))

The problem is when I want to create another list (large list) of values of f[x]. It seems that Total is what slows things down here and I was wondering if there is a way to replace Total with something faster.

Any clues?

  • $\begingroup$ Are A1 and A2 different each time you call f or are they constant? $\endgroup$
    – wxffles
    Oct 11, 2012 at 21:26
  • 1
    $\begingroup$ Mine is very fast, I can run about 10000 x values per second. What hardware are you using? $\endgroup$
    – s0rce
    Oct 11, 2012 at 21:50
  • 1
    $\begingroup$ @user72409 actually, what are the exact sizes of A1 and A2? You said 2D Array, we've been using single arrays in answers... $\endgroup$
    – tkott
    Oct 11, 2012 at 21:59
  • 1
    $\begingroup$ A1 and A2 are 2500x2500 (but this is an example, it could be more but let's go with this because I know that it takes about 10 mins on my machine). But as I said before, these remain constant. This was the whole idea. I wanted to precalculate them in order to gain speed, and I did but the final Table[f[x],{x,1,2500}], this takes 10 mins to calculate. $\endgroup$
    – lucian
    Oct 11, 2012 at 22:06
  • 2
    $\begingroup$ 6,000,000 >> 2000 $\endgroup$
    – wxffles
    Oct 11, 2012 at 22:09

3 Answers 3


I suspect that your arrays a1 and a2 are not packed, which would case a considerable slow-down.


f[x_] := Total[a1/(a2 + x), -1]

n = Ceiling @ Sqrt @ 2000;

a1 = RandomReal[{-99, 99}, {n, n}];
a2 = RandomReal[{-99, 99}, {n, n}];

Developer`PackedArrayQ /@ {a1, a2}

{True, True}

Table[f[i], {i, 1, 50000}] // AbsoluteTiming // First


{a1, a2} = Developer`FromPackedArray /@ {a1, a2};

Table[f[i], {i, 1, 50000}] // AbsoluteTiming // First


f[x_, a1_, a2_] := Total[a1/(a2 + x), Infinity];
{a1, a2} = RandomReal[{0, 1}, {2, 2000}];
Timing[f[#, a1, a2] & /@ RandomReal[{0, 1}, 2000]][[1]]

Takes under one second in my machine

  • $\begingroup$ Odd, I tried f[x_] = Total[a1/(a2 + x), Infinity] followed by Timing[f[#] & /@ RandomReal[{0, 1}, 2000]][[1]] and it is about 4 seconds on my machine vs 0.25 for your solution. I would have thought that precomputing the Total would have been faster. Any ideas? $\endgroup$
    – tkott
    Oct 11, 2012 at 21:56
  • 1
    $\begingroup$ @tkott loss of packing $\endgroup$
    – Mr.Wizard
    Oct 11, 2012 at 22:06
  • $\begingroup$ @tkott evaluate SetSystemOptions[PackedArrayOptions->UnpackMessage->True] and you'll see :) $\endgroup$
    – acl
    Oct 11, 2012 at 22:07
  • $\begingroup$ @acl or shorter: On["Packing"] $\endgroup$
    – Mr.Wizard
    Oct 11, 2012 at 22:11
  • $\begingroup$ @Mr.Wizard, @ acl ahh, thanks $\endgroup$
    – tkott
    Oct 12, 2012 at 13:42

Have you tried Table instead of ParallelTable?

Anyway, here's an ugly way to get f a bit faster:

a1 = RandomReal[{0.1, 10}, 10000];
a2 = RandomReal[{0.1, 10}, 10000];

Original definition:

f[x_] := Total[a1/(a2 + x), Infinity]

slightly faster:

fc = Compile[
   {{x, _Real}, {a1, _Real, 1}, {a2, _Real, 1}},
   Module[{t = 0.},
    Do[t = t + a1[[i]]/(a2[[i]] + x), {i, 1, Length[a1]}];
   CompilationTarget -> "C"

Table[f[N@x], {x, 1, 2000}]; // Timing
Table[fc[N@x, a1, a2], {x, 1, 2000}]; // Timing
{0.370212, Null}
{0.164774, Null}

One possibility as to why ParallelTable is slower: it unpacks. Try:

  "PackedArrayOptions" -> "UnpackMessage" -> True];

a1 = RandomReal[{0.1, 10}, 10000];
a2 = RandomReal[{0.1, 10}, 10000];

ParallelTable[f[x], {x, 1, 2000}]; // AbsoluteTiming
Table[f[x], {x, 1, 2000}]; // AbsoluteTiming

Mathematica graphics

There is also added overhead when using ParallelTable. As tkott mentions in a comment, it would be helpful to know what a1 and a2 actually are (their dimensions).

  • 1
    $\begingroup$ you forgot the Total part :) $\endgroup$
    – tkott
    Oct 11, 2012 at 21:53
  • $\begingroup$ I am afraid that I do not understand how you made f faster. Can you explain a bit more please? $\endgroup$
    – lucian
    Oct 11, 2012 at 22:10
  • $\begingroup$ @user72409 sorry I botched the pasting and did not actually paste the faster version. In any case I don't know if this will remain faster for such large arrays (and it needs modification to work with 2D arrays anyway; perhaps you could mention this in your question!) $\endgroup$
    – acl
    Oct 11, 2012 at 22:14
  • $\begingroup$ @acl Thank you fo the extensive post. I have never used Compile successfully but I will try and give it another go. $\endgroup$
    – lucian
    Oct 11, 2012 at 22:27
  • $\begingroup$ @acl I tried running my problem using Table instead of ParallelTable but the latter is considerably faster. I get 649 seconds vs a lot more for the case of Table (I actually stopped it after it passed the 10 minute mark) $\endgroup$
    – lucian
    Oct 11, 2012 at 22:31

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.