Below is a simple example to illustrate the problem
test = Compile[{{n, _Integer}}, n]
Now, Table
works fine
Table[test[n], {n, 1, 10}]
But NSum
NSum[test[n], {n, 1, 10}]
gives errors as
CompiledFunction::cfsa: Argument n at position 1 should be a machine-size integer. >>
What does it mean?
Update
Simon Woods suggested that we could simply ignore this warning. But I don't think so. See the timing below.
In[2]:= NSum[test[n], {n, 1, 100000}] // AbsoluteTiming
During evaluation of In[2]:= CompiledFunction::cfsa: Argument n at position 1 should be a machine-size integer. >>
During evaluation of In[2]:= CompiledFunction::cfsa: Argument n at position 1 should be a machine-size integer. >>
Out[2]= {0.270994, 5.00005*10^9}
In[4]:= Sum[test[n], {n, 1, 100000}] // AbsoluteTiming
Out[4]= {0.114388, 5000050000}
NSum
is significantly slower than Sum
NSum[Hold@test[n],{n,1,10}]
or create a wrapper function fortest
which only lets through the correct type of argument e.g.test2[n_Integer]:=test[n]
$\endgroup$Quiet@NSum[test[n], {n, 1, 100000}] // RepeatedTiming
than I do fromQuiet@Sum[test[n], {n, 1, 100000}] // RepeatedTiming
$\endgroup$NSum
is about 20 times faster thanSum
$\endgroup$NSum
is slower. I thinkRepeatedTiming
is lying, maybe it is using caching? UsingAbsoluteTiming
Nsum
is slower on my computer $\endgroup$Quiet
as shown by Jason the message is suppressed andNSum
is much faster. When I said you could ignore the warning, I meant that the result would be correct. If it's important to save that fraction of a second then don't ignore it. $\endgroup$