       Strange timing of many assignments

• To: mathgroup at smc.vnet.net
• Subject: [mg77846] Strange timing of many assignments
• From: Fred Simons <f.h.simons at tue.nl>
• Date: Mon, 18 Jun 2007 06:57:52 -0400 (EDT)

In the following example I do a lot of assignments to the same symbol s.
(Mathematica 6, Windows Vista) One might expect that the time for doing
so is linear in the number of assignments, but in the example it is not.
Can anyone shed a light on this?

In:= Table[
lst=RandomChoice[{a,b,c,d,e,f},  i  10^4];
Timing[Do[s=lst[[i]], {i,1,Length[lst]}]][],
{i,1,5}]

Out= {0.811,2.855,6.411,11.357,17.628}

While the first 10000 assignments were done in less than a second, the
last 10000 needed more than 6 seconds.

If we do not use the list, the behaviour is as expected:

In:= Table[
Timing[
Do[s=RandomChoice[{a,b,c,d,e,f}], {i,1,1 10^4}]][],
{i,1,5}]

Out= {0.047,0.047,0.047,0.031,0.031}

So it looks like retrieving the elements from the list is responsible
for the delay. But that is not true either:

In:= Table[
lst=RandomChoice[{a,b,c,d,e,f},  i  10^4];
Timing[Do[lst[[i]], {i,1,Length[lst]}]][],
{i,1,5}]

Out= {0.031,0.031,0.047,0.062,0.031}

Fred Simons
Eindhoven University of Technology

• Prev by Date: Re: question
• Next by Date: Re: Trying to export two column table to text file
• Previous by thread: Re: Rule and Module not working together
• Next by thread: Re: more gripe about the new documentation center (DC) in Mathematica