What to use for Python stacks and queues (speed comparison of each data structure)

Introduction

This article is mainly about competitive programming.

There are multiple ways to implement stacks and queues in Python.

--Stack --Use list (ʻappend () , pop () ) --Use collections.deque (ʻappend () ,pop ()) --Queue --Use list (ʻappend () , pop (0) ) --Use collections.deque (ʻappend () ,popleft ()) --Use queue.Queue (put (), get ())

By the way, which of these is the best to use? This time, we will focus on the speed aspect.

Measurement method

Add and retrieve elements 10 times, 100 times, 1000 times, 10000 times, 100000 times for each data structure.

All code omits the import part below.

import part


from collections import deque
from queue import Queue
import time

stack

use list


# stack(list)
stack_list_append = []
stack_list_pop = []
for i in range(1, 6):
    start = time.time()

    stack_list = []
    for j in range(10 ** i):
        stack_list.append(j)

    stack_list_append.append(time.time() - start)

    start = time.time()

    for j in range(10 ** i):
        stack_list.pop()

    stack_list_pop.append(time.time() - start)

use deque


# stack(deque)
stack_deque_append = []
stack_deque_pop = []
for i in range(1, 6):
    start = time.time()

    stack_deque = deque([])
    for j in range(10 ** i):
        stack_deque.append(j)

    stack_deque_append.append(time.time() - start)

    start = time.time()

    for j in range(10 ** i):
        stack_deque.pop()

    stack_deque_pop.append(time.time() - start)

queue

use list


# queue(list)
queue_list_append = []
queue_list_pop = []
for i in range(1, 6):
    start = time.time()

    queue_list = []
    for j in range(10 ** i):
        queue_list.append(j)

    queue_list_append.append(time.time() - start)

    start = time.time()

    for j in range(10 ** i):
        queue_list.pop(0)

    queue_list_pop.append(time.time() - start)

use deque


# queue(deque)
queue_deque_append = []
queue_deque_pop = []
for i in range(1, 6):
    start = time.time()

    queue_deque = deque([])
    for j in range(10 ** i):
        queue_deque.append(j)

    queue_deque_append.append(time.time() - start)

    start = time.time()

    for j in range(10 ** i):
        queue_deque.popleft()

    queue_deque_pop.append(time.time() - start)

Use Queue


# queue(Queue)
queue_Queue_append = []
queue_Queue_pop = []

for i in range(1, 6):
    start = time.time()

    queue_Queue = Queue()
    for j in range(10 ** i):
        queue_Queue.put(j)

    queue_Queue_append.append(time.time() - start)

    start = time.time()

    for j in range(10 ** i):
        queue_Queue.get()

    queue_Queue_pop.append(time.time() - start)

Measurement result

When the measurement result is graphed, it is as follows.

スクリーンショット 2020-03-01 16.35.38.png

Let's start from the upper left.

Stack result

Add element

The graph on the upper left. スクリーンショット 2020-03-01 16.39.29.png The deque is slightly faster, but it's about the same.

Extracting elements

The graph on the upper right. スクリーンショット 2020-03-01 16.40.06.png Again, the deque is slightly faster, but it's about the same.

Queue result

Add element

The lower left graph. スクリーンショット 2020-03-01 16.40.40.png With a large number of elements, the Queue is more than 10 times slower than the others.

Extracting elements

The graph at the bottom right. スクリーンショット 2020-03-01 16.40.48.png The Queue is the same as when adding an element, but the list is obviously bad. It's more than 100 times slower than the fastest deque.

Conclusion

It's best to use collections.deque for both stacks and queues.

Since it's a big deal, I'll write a simple usage.

stack

from collections import deque

s = deque([])
s.append(1)  # [1]
s.append(2)  # [1, 2]
s.append(3)  # [1, 2, 3]
s.pop()      #Remove from top[1, 2, 3] -> [1, 2]
s.pop()      # [1, 2] -> [1]
s.pop()      # [1] -> []

queue

In the stack, it was pop when it was removed, but in the queue it is just popleft.

from collections import deque

q = deque([])
q.append(1)  # [1]
q.append(2)  # [1, 2]
q.append(3)  # [1, 2, 3]
q.popleft()  #Remove from the bottom[1, 2, 3] -> [2, 3]
q.popleft()  # [2, 3] -> [3]
q.popleft()  # [3] -> []

Recommended Posts

What to use for Python stacks and queues (speed comparison of each data structure)
Comparison of how to use higher-order functions in Python 2 and 3
How to use "deque" for Python data
Use data class for data storage of Python 3.7 or higher
List of Python libraries for data scientists and data engineers
Python netCDF4 read speed and nesting of for statements
[Introduction to Azure for kaggle users] Comparison of how to start and use Azure Notebooks and Azure Notebooks VM
[Python] How to set variable names dynamically and speed comparison
Python application: Data cleansing # 3: Use of OpenCV and preprocessing of image data
[Python] Summary of how to use split and join functions
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
processing to use notMNIST data in Python (and tried to classify it)
Speed comparison of Wiktionary full text processing with F # and Python
I want to use both key and value of Python iterator
Speed comparison of Python XML parsing
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
[Python of Hikari-] Chapter 05-09 Control syntax (use of for statement and while statement properly)
I measured the speed of list comprehension, for and while with python2.7.
[Python] Summary of how to use pandas
How to install and use pandas_datareader [Python]
Python data structure and internal implementation ~ List ~
[Python] Organizing how to use for statements
Python data structure and operation (Python learning memo ③)
[Python2.7] Summary of how to use unittest
python: How to use locals () and globals ()
How to use Python zip and enumerate
Compress python data and write to sqlite
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
How to use is and == in Python
[Introduction to Data Scientists] Basics of Python ♬
Speed comparison of murmurhash3, md5 and sha1
[Question] How to use plot_surface of python
To speed up python, summarize the amount of calculation of collection type (list / tuple / dictionary / set) for each purpose.
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
Data analysis in Python Summary of sources to look at first for beginners
OpenGoddard How to use 2-python library for nonlinear optimal control and orbit generation
Tips for those who are wondering how to use is and == in Python
How to use OpenGoddard 3-python library for nonlinear optimal control and orbit generation
How to use OpenGoddard 4-python library for nonlinear optimal control and orbit generation
How to use Service Account OAuth and API with Google API Client for python
Organize Python tools to speed up the initial movement of data analysis competitions
How to use OpenGoddard 1-python library for nonlinear optimal control and orbit generation
[Introduction to Python] How to get the index of data with a for statement
[Python] How to use two types of type ()
[Introduction to statistics] What kind of distribution is the t distribution, chi-square distribution, and F distribution? A little summary of how to use [python]
[Python3] Coarse graining of numpy.ndarray Speed comparison etc.
[Python] How to read data from CIFAR-10 and CIFAR-100
Use decorators to prevent re-execution of data processing
Summary of how to use MNIST in Python
How to use data analysis tools for beginners
Compare the speed of Python append and map
python development environment -use of pyenv and virtualenv-
[Python] How to use hash function and tuple.
Speed: Add element to end of Python array
Summary of studying Python to use AWS Lambda
Comparison of R and Python writing (Euclidean algorithm)
List of Python code to move and remember
Data cleansing 3 Use of OpenCV and preprocessing of image data
Comparison of Python and Ruby (Environment / Grammar / Literal)