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[133]:= Table[ lst=RandomChoice[{a,b,c,d,e,f}, i 10^4]; Timing[Do[s=lst[[i]], {i,1,Length[lst]}]][[1]], {i,1,5}] Out[133]= {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[138]:= Table[ Timing[ Do[s=RandomChoice[{a,b,c,d,e,f}], {i,1,1 10^4}]][[1]], {i,1,5}] Out[138]= {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[139]:= Table[ lst=RandomChoice[{a,b,c,d,e,f}, i 10^4]; Timing[Do[lst[[i]], {i,1,Length[lst]}]][[1]], {i,1,5}] Out[139]= {0.031,0.031,0.047,0.062,0.031} Fred Simons Eindhoven University of Technology