A timing puzzle
- To: mathgroup at smc.vnet.net
- Subject: [mg45163] A timing puzzle
- From: Richard Gass <gass at physics.uc.edu>
- Date: Fri, 19 Dec 2003 06:57:29 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Can anyone explain the following timing results. In[1]:= Table[First[Sum[i,{i,1,10^i}]//Timing],{i,1,12}] Out[1]= {0. Second,0. Second,0. Second,0.02 Second,0.36 Second,4.02 Second,0.19 \ Second,0.04 Second,0.04 Second,0.04 Second,0.04 Second,0.04 Second} For upper limits large than 10^6 the time drops dramatically. You can check this by comparing Sum[i, {i, 1, 10^6 + 1}]//Timing with Sum[i, {i, 1, 10^6}]//Timing. I suspect that what is happing is that for upper limits less than 10^6 Sum does the sum by adding the numbers but for upper limits larger than 10^6 Sum does the computation with a symbolic upper limit to get In[6]:= Sum[i,{i,1,j}] Out[6]= j (1 + j) --------- 2 and then evaluates the upper limit. This would explain the constant times from i=10^7 to i=10^12. Can anyone confirm or refute this?