[Python-Dev] type(obj) vs. obj.class (original) (raw)
Peter Ludemann pludemann at google.com
Sun Oct 18 20:57:19 EDT 2015
- Previous message (by thread): [Python-Dev] type(obj) vs. obj.__class__
- Next message (by thread): [Python-Dev] type(obj) vs. obj.__class__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 18 October 2015 at 17:41, Chris Angelico <rosuav at gmail.com> wrote:
On Mon, Oct 19, 2015 at 11:35 AM, David Mertz <mertz at gnosis.cx> 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() usingself.max==len(self.data)
> if you prefer that approach. I doubtmyringbuffer.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, sizemax): > self.data = [None] * sizemax > self.sizemax = sizemax > self.used = 0 > self.cur = 0 > def append(self, val): > self.data[self.cur] = val > self.cur = (self.cur+1) % self.sizemax > self.used = max(self.used, self.cur+1) > def isfull(self): > self.used == self.sizemax > > Feel free to try this version against whatever benchmark you have in mind.What does this provide that collections.deque(maxlen=sizemax) 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 Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/pludemann%40google.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20151018/155a9ee4/attachment.html>
- Previous message (by thread): [Python-Dev] type(obj) vs. obj.__class__
- Next message (by thread): [Python-Dev] type(obj) vs. obj.__class__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]