efficient idiomatic queue?

Raymond Hettinger othello at javanet.com
Thu Jan 17 08:04:02 EST 2002


Delaney, Timothy" <tdelaney at avaya.com> wrote in message
news:mailman.1011232764.16285.python-list at python.org...

> class DictLongFifo:
>
> def __init__(self):
> self.data = {}
> self.nextin = long(0)
> self.nextout = long(0)
>
> def append(self, value):
>
> self.data[self.nextin] = value
> self.nextin += 1
>
> def pop(self, pos=-1):
> value = self.data[self.nextout]
> del self.data[self.nextout]
> self.nextout += 1
> return value

I'm not sure why, but subclassing the built-in types gives amazing
performance
benefits.  Please re-try the timings with this version of DictLongFifo:

class DictLongFifo(dict):
    def __init__(self):
        self.nextin = 0L
        self.nextout = 0L
    def append(self, value):
        self[self.nextin] = value
        self.nextin += 1
    def pop(self):
        value = self[self.nextout]
        del self[self.nextout]
        self.nextout += 1
        return value


Raymond Hettinger





More information about the Python-list mailing list