MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: local variables - Module, For loop


Am 12.10.2010 19:52, schrieb Sebastian Schmitt:
> Bill Rowe wrote:
>> In[5]:= Timing[Sum[k, {k, n}]]
>>
>> Out[5]= {0.393106,500000500000}
>>
>> In[6]:= Timing[Sum[k, {k, m}] /. m -> n]
>>
>> Out[6]= {0.08371,500000500000}
>>
>> Each code snippet does exactly the same thing and produces
>> exactly the same sum. But as you can see, using For is the
>> slowest method.
> 
> Dear Bill!
> 
> Thank you very much for your detailed answer!
> 
> Interesting that the last version is so much faster. Is it only due to
> numeric vs. algebraic?

the first is adding a lot of numbers, the second is evaluating 1/2
m(m+1). It's hard to believe, but there was a 9 year old boy named Carl
Friedrich who made the experience that the second is faster some 200
years ago -- without access to Mathematica!!!

:-)

hth,

albert




  • Prev by Date: Re: Graphics3D. How to join random points with a single
  • Next by Date: Re: Boolean factorization
  • Previous by thread: Re: local variables - Module, For loop
  • Next by thread: Re: local variables - Module, For loop