Execution speed question

Iain King iainking at gmail.com
Fri Jul 25 14:46:56 CEST 2008


On Jul 25, 10:57 am, Suresh Pillai <stochash... at yahoo.ca> wrote:
> I am performing simulations on networks (graphs).  I have a question on
> speed of execution (assuming very ample memory for now).  I simplify the
> details of my simulation below, as the question I ask applies more
> generally than my specific case.  I would greatly appreciate general
> feedback in terms of computing and of course considerations specific to
> implementation in Python.
>
> The nodes in my network may be ON or OFF.  The network starts off with
> all nodes in the OFF state.  I loop through the nodes.  For each node
> that is OFF, I consider some probability of it turning ON based on the
> states of its neighbours.  I MUST GO THROUGH ALL NODES BEFORE DECIDING
> WHICH ONES TO TURN ON.
>
> So my question is whether it is faster to
>
> 1. loop through a list of ALL nodes and check for OFF nodes using ifs
>
> or to
>
> 2. loop through a container of OFF nodes and remove from this when they
> turn ON

or 3. build a new list every iteration intead of deleting from the old
one:

while processing:
    new_off_list = []
    for x in off_list:
        if goes_on(x):
            on_list.append(x)
        else:
            new_off_list.append(x)
    off_list = new_off_list
    generation += 1

Iain



More information about the Python-list mailing list