Queues - Is Infinity all right?

Rob Hunter rob at cs.brown.edu
Sun Oct 5 17:19:42 CEST 2003


On Sunday, October 5, 2003, at 09:20 AM, Anand Pillai wrote:

> The standard Python Queue module, allows to generate queues that
> have no size limit, by passing the size argument as <= 0.
>
> q = Queue(0)
>
> In a multithreaded application, these queues could be useful
> when you have many threads using the queue for data access
> and synchronization.
>
> Is there a performance hit when one uses an 'infinite' queue
> when compared to a queue of fixed size?

No.  There is no performance hit for using infinite queues so far as I 
can tell from reading the source code.  (Now, of course, bigger queue 
== more memory == potential slowdowns, but that's different.)  In fact, 
technically, having an infinite queue might even be even faster than 
bounding the size because of this function:

   # Check whether the queue is full
     def _full(self):
         return self.maxsize > 0 and len(self.queue) == self.maxsize

Whenever you "get" or "put" into the queue, you have to call this 
function.  And, since, "and" is short-circuiting, for an infinite 
queue, it will always return false for "self.maxsize > 0" and therefore 
never have to evaluate the second part of the and.


>
> Of course it should depend upon the O(n) of the Queue data structure,
> how the time of access (get/put) varies with the number of items in
> it.

But it doesn't depend on such things.  There is no cost.  Check out the 
source code if you like.  On my computer it's in /usr/lib/python<some 
version>/Queue.py

Rob






More information about the Python-list mailing list