Re: [Python-Dev] type(obj) vs. obj.__class__

2015年10月18日 18:07:29 -0700

On 18 October 2015 at 17:41, Chris Angelico <[email protected]> wrote:
> On Mon, Oct 19, 2015 at 11:35 AM, David Mertz <[email protected]> wrote:
> > That's interesting about the `self._full` variable slowing it down, I
> think
> > I'm not surprised (but obviously it depends on just how it's used). But
> one
> > can also simply define RingBuffer.isfull() using
> `self.max==len(self.data)`
> > if you prefer that approach. I doubt `myringbuffer.isfull()` is
> something
> > you need to call in an inner loop.
> >
> > That said, I think my implementation of RingBuffer would probably look
> more
> > like (completely untested):
> >
> > class RingBuffer(object):
> > def __init__(self, size_max):
> > self.data = [None] * size_max
> > self.size_max = size_max
> > self.used = 0
> > self.cur = 0
> > def append(self, val):
> > self.data[self.cur] = val
> > self.cur = (self.cur+1) % self.size_max
> > self.used = max(self.used, self.cur+1)
> > def isfull(self):
> > self.used == self.size_max
> >
> > Feel free to try this version against whatever benchmark you have in
> mind.
>
> What does this provide that collections.deque(maxlen=size_max)
> doesn't? I'm a little lost.
>
​I was merely re-implementing the "clever" code in a slightly less clever
way, for the same performance, to demonstrate that there's no need to
assign to __class__.
collections.deque is about 5x faster.
(My simple benchmark tests the cost of x.append(i))
- p​
>
> ChrisA
> _______________________________________________
> Python-Dev mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/pludemann%40google.com
>
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to