my own type in C, sequence protocol

Alex Martelli aleaxit at yahoo.com
Thu Sep 9 16:34:22 EDT 2004


Brandon Craig Rhodes <brandon at ten22.rhodesmill.org> wrote:
   ...
> > You might want your type's init function to accept a
> > sequence, to copy for an initial value, as in
> >
> >  m = myType([1, 2, 3])
> 
> Note, however, that this requires Python to create a list with the
> values 1, 2, and 3 in it; then pass that value to your init function;
> and finally discard the list when the initializer has completed.

Yeah, so?

> Perhaps more efficient (maybe you could test both to compare the
> expense?) would be for your initialization function to accept the new
> sequence members as arguments themselves:
> 
>     m = myType(1, 2, 3)
> 
> This way a list does not have to be created and destroyed, with all
> the associated computation, each time you initialize a myType.

Yeah, but a tuple (of arguments) still has to be.

Sure, there IS a performance difference, but it's very small...:

kallisti:~ alex$ python cb/timeit.py -s'
> class mt:
>   def __init__(self, seq): self.seq=list(seq)
> ' 'mt([ 1,2,3 ])'
100000 loops, best of 3: 9.52 usec per loop

kallisti:~ alex$ python cb/timeit.py -s'
class mt:
  def __init__(self, *seq): self.seq=list(seq)
' 'mt(1,2,3)'
100000 loops, best of 3: 8.38 usec per loop
kallisti:~ alex$ 

You're not going to create enough instances to care about that
microsecond's difference (and that's on a modest 800 MHz iBook G4 cheap
laptop a year old...).  To me, it seems very much like a case where
design decisions should be based on clarity and simplicity, not on tiny
performance advantages...!


Alex



More information about the Python-list mailing list