Execution speed question

Suresh Pillai stochashtic at yahoo.ca
Mon Jul 28 03:40:19 EDT 2008


On Fri, 25 Jul 2008 08:08:57 -0700, Iain King wrote:

> On Jul 25, 3:39 pm, Suresh Pillai <stochash... at yahoo.ca> wrote:
>> That's a good comparison for the general question I posed.  Thanks.
>> Although I do believe lists are less than ideal here and a different
>> data structure should be used.
>>
>> To be more specific to my case:
>> As mentioned in my original post, I also have the specific condition
>> that one does not know which nodes to turn ON until after all the
>> probabilities are calculated (lets say we take the top m for example).
>> In this case, the second and third will perform worse as the second one
>> will require a remove from the list after the fact and the third will
>> require another loop through the nodes to build the new list.
> 
> So you need to loops through twice regardless?  i.e. loop once to gather
> data on off nodes, do some calculation to work out what to turn on, then
> loop again to turn on the relevant nodes?  If so, then I think the
> functions above remain the same, becoming the 2nd loop. Every iteration
> you do a first loop over the off_nodes (or them all for (1)) to gather
> the data on them, perform your calculation, and then perform one of the
> above functions (minus the setup code at the begining; basically
> starting at the 'for') as a second loop, with the goes_on function now
> returning a value based on the calculation (rather than the calculation
> itself as I had it).  Performance should be similar.
> 
> Iain

If do I settle on an explicit loop to remove the nodes turned ON, then I 
realised this weekend that I could do this in the next iteration of the 
simulation (first loop above) and save some iteration overhead (the if 
checking will still be there of course).

And thanks for pointing out that constructing a new list, for long lists, 
is faster than simple removal.  It's obvious but I never really thought 
of it; good tip.



More information about the Python-list mailing list