Re: local variables - Module, For loop
- To: mathgroup at smc.vnet.net
- Subject: [mg113117] Re: local variables - Module, For loop
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Wed, 13 Oct 2010 12:40:40 -0400 (EDT)
- References: <i9279n$3i3$1@smc.vnet.net>
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
- Follow-Ups:
- Re: local variables - Module, For loop
- From: Sebastian Schmitt <sschmitt@physi.uni-heidelberg.de>
- Re: local variables - Module, For loop