# Can you understand this speed up to NSum?

**URL:** <https://community.wolfram.com/t/can-you-understand-this-speed-up-to-nsum/7782>\
**Category:** Questions\
**Tags:** mathematica, numerical-computation\
**Created:** [January 29, 2016, 4:07am UTC](https://community.wolfram.com/t/can-you-understand-this-speed-up-to-nsum/7782 "2016-01-29T04:07:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmmmburns](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/bmmmburns/32/3126_2.png) [@bmmmburns](https://community.wolfram.com/u/bmmmburns)\
**Post date:** [January 29, 2016, 4:07am UTC](https://community.wolfram.com/t/can-you-understand-this-speed-up-to-nsum/7782/1 "2016-01-29T04:07:20Z")

</div>

Here is a strange way to increase the speed of NSum. A dummy second summation (below, {n, 1, 1}) nearly halves the timing!! Can anyone figure out why it works?

## Can anyone at Wolfram Research figure out why?

```
 g[x_] := (x^(1/x) - 1);
  
m = NSum[(-1)^x (g[x]), {x, 1, Infinity}, 
   Method -> "AlternatingSigns", WorkingPrecision -> 10000];

   
  N[Timing[m - 
   NSum[(-1)^x (g[x]), {x, 1, Infinity}, Method -> "AlternatingSigns",
     WorkingPrecision -> 10000, NSumTerms -> 100]], 50]

 (*{88.75, 0.*10^-9998}*)

   
  (* In[197]:=*) N[
   Timing[m - 
     NSum[(-1)^x (g[x]), {x, 1, Infinity}, {n, 1}, 
      Method -> "AlternatingSigns", WorkingPrecision -> 10000, 
      NSumTerms -> 100]], 50]
  
 (* Out[197]= {47.7969, 0.*10^-9998}*)

```

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [January 29, 2016, 11:51pm UTC](https://community.wolfram.com/t/can-you-understand-this-speed-up-to-nsum/7782/2 "2016-01-29T23:51:12Z")

</div>

It’s a bit tricky and I’m not certain I have all the issues correct. Has to do with when and where the approximate numbers are computed. The first one substituted bignums of high precision into the formula for `g[x]`. The second, because it has that extra layer, fools `NSum` into believing it must treat the function as a “black box”, and it substitutes the exact integers before numericizing. For reasons not clear to me, it is faster to do this and numericize the resulting expression after the fact (even with evaluating the exact roots). Also there could be differences in how many terms get used based on the use, or not, of the explicit form of the function.

I think this next example more or less captures what is happening.

```
g1[x_] := N[(x^(1/x) - 1), 10000];
g2[x_] := With[{nx = N[x, 10000]}, (nx^(1/nx) - 1)]

m = 1000;
vals = Range[m];

ClearSystemCache["Numeric"];
Timing[res1 = Map[g1, vals];]
ClearSystemCache["Numeric"];
Timing[res2 = Map[g2, vals];]

(* Out[363]= {2.019356, Null}

Out[365]= {5.185261, Null} *)

```
