Asynchronous Messaging

wink wink at saville.com
Thu Sep 27 06:43:32 CEST 2007


Fredrik,

You are most correct, but Queue is slow compared to deque but
not for the reason I guessed. Apparently it's because deque is
implemented in C while Queue is in python. Using the program below
it looks there is about a 35:1 speed difference.

            100 d.append  0.000011s  0.1097us per
            100 d.popL    0.000011s  0.1097us per
            100 q.put     0.000429s  4.2892us per
            100 q.get     0.000391s  3.9077us per

So someday it _might_ warrant adding Queue to collections.


#!/usr/bin/python

import timeit
import Queue
from collections import deque

setupD = """
from collections import deque
d=deque()
cnt = %d
for x in xrange(cnt):
    d.append(x)
"""

setupQ = """
import Queue
q=Queue.Queue()
cnt = %d
for x in xrange(cnt):
    q.put(x)
"""

def main():
    cnt = 100
    t = timeit.Timer(setup=setupD % cnt, stmt="d.append(0)")
    time = min(t.repeat(10000, cnt))
    print "      %9d d.append  %fs %7.4fus per" % \
            (cnt, time, (time/cnt) * 1000000.0)

    t = timeit.Timer(setup=setupD % cnt, stmt="d.popleft()")
    time = min(t.repeat(10000, cnt))
    print "      %9d d.popL    %fs %7.4fus per" % \
            (cnt, time, (time/cnt) * 1000000.0)

    t = timeit.Timer(setup=setupQ % cnt, stmt="q.put(0)")
    time = min(t.repeat(10000, cnt))
    print "      %9d q.put     %fs %7.4fus per" % \
            (cnt, time, (time/cnt) * 1000000.0)

    t = timeit.Timer(setup=setupQ % cnt, stmt="q.get()")
    time = min(t.repeat(10000, cnt))
    print "      %9d q.get     %fs %7.4fus per" % \
            (cnt, time, (time/cnt) * 1000000.0)
if __name__ == "__main__":
    main()




More information about the Python-list mailing list