Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Out of sheer curiosity, I integrated my own answer with your original code and F.J.'s solutions to make a comparative performance test.</p> <p>It seems that your solution is the fastest of them all. My solution check all possible elements of the controls tuple, so it will be slower as the size of the tuple increases.</p> <p>Here is the code:</p> <pre><code>from timeit import Timer as T from itertools import chain, dropwhile class control(object): def __init__(self, name): self.name = name def johan_venge(tuple_): for el in tuple_: if el.name == 'foobar': return el return None def mac(tuple_): return filter(lambda x : x.name == 'foobar', tuple_)[0] def mac2(tuple_): return list(dropwhile(lambda x : x.name != 'foobar', tuple_))[0] def fj(tuple_): return next(c for c in tuple_ if c.name == 'foobar') def fj2(tuple_): return next(chain((c for c in tuple_ if c.name == 'foobar'))) if __name__ == '__main__': REPS = 10000 controls = (control('hello'), control('world'), control('foobar')) print T(lambda : johan_venge(controls)).repeat(number = REPS) print T(lambda : mac(controls)).repeat(number = REPS) print T(lambda : mac2(controls)).repeat(number = REPS) print T(lambda : fj(controls)).repeat(number = REPS) print T(lambda : fj2(controls)).repeat(number = REPS) </code></pre> <p>and here is the output on my system:</p> <pre><code>[0.005961179733276367, 0.005975961685180664, 0.005918025970458984] [0.013427019119262695, 0.013586044311523438, 0.013450145721435547] [0.024325847625732422, 0.0254058837890625, 0.02396702766418457] [0.014491081237792969, 0.01442408561706543, 0.01484990119934082] [0.01691603660583496, 0.016616106033325195, 0.016437053680419922] </code></pre> <p>HTH! :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload