I am practicing doing programming competitions using python(actually pypy3). For a problem I need to use a queue - I only need to put at one end and pop from the other. From what I found in the documentation it seems I have two options queue.Queue
and queue.deque
. I first tried the problem using queue.Queue, but my solution exceeded the time limit. Then I switched to queue.deque
and I passed the problem(though close to the limit).
From the documentation it seems both containers are thread safe(well at least for some operations for deque) while for my situation I will never use more than one thread. Is there a simple non-synchronized queue built-in in python?
The deque
certainly does not do synchronization; the documentation just states that the appends and pops are guaranteed to be thread-safe due to them being atomic. In CPython in particular there is no locking besides the Global Interpreter Lock. If you need a double-ended queue, or say FIFO, that is what you should use. If you need a LIFO stack, use a list. Internally the deque
implementation uses a doubly-linked list of fixed-length blocks.
The queue.Queue
uses a deque
internally; in addition, it uses a mutex to guard those remaining operations that are not implemented atomically by deque
.
Thus your problem is not the choice of deque
, but quite probably some other aspect of your algorithm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With