![]() ![]() You can also see that even in such cases you're only wasting 5 microseconds or so per append/pop pair and decide how significant that wastage is (though if that's all you're doing with that container, deque has no downside, so you might as well switch even if 5 usec more or less won't make an important difference).įrom Beazley's Python Essential Reference, Fourth Edition, p. You can see that the claim of O(1) performance for deque is well founded, while a list is over twice as slow around 1,000 items, an order of magnitude around 10,000. $ python -mtimeit -s'import collections q=que(range(10000))' 'q.append(23) q.popleft()'ġ000000 loops, best of 3: 0.574 usec per loop (not very different for 12 instead of 100 items btw), and in much-larger ones: $ python -mtimeit -s'q=range(10000)' 'q.append(23) q.pop(0)'ġ00000 loops, best of 3: 5.81 usec per loop $ python -mtimeit -s'import collections q=que(range(100))' 'q.append(23) q.popleft()'ġ000000 loops, best of 3: 0.576 usec per loop Python -mtimeit is your friend - a really useful and simple micro-benchmarking approach! With it you can of course also trivially explore performance in much-smaller cases: $ python -mtimeit -s'q=range(100)' 'q.append(23) q.pop(0)'ġ000000 loops, best of 3: 0.972 usec per loop $ python -mtimeit -s'import collections q=que(range(1000))' 'q.append(23) q.popleft()'ġ000000 loops, best of 3: 0.573 usec per loop Some answers claimed a "10x" speed advantage for deque vs list-used-as-FIFO when both have 1000 entries, but that's a bit of an overbid: $ python -mtimeit -s'q=range(1000)' 'q.append(23) q.pop(0)'ġ000000 loops, best of 3: 1.24 usec per loop ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |