I checked the reference speed when using python list, dictionary, and set type in.

Introduction

The following site describes the following. Element validation (O (1)) using sets and dictionaries is faster than array retrieval (O (n)). When looking up a in b, b should be sets or dictionaries, not lists or tuples. http://www.peignot.net/python-speed

Is it true? How different is it from what I thought? I was curious, so I actually tried it.

Execution environment

python2.7 windows7 Intel Core i5 CPU 2.4GHz Memory 4.0 GB

Execution condition

In a list, dictionary, and set type, I made an integer sequence (1 million elements) for each multiple of 1, every multiple of 2, and every multiple of 3, and referenced in in from 1 to 10,000. The reason why we created integer sequences for each multiple of 1, every multiple of 2, and every multiple of 3 is to investigate the effect of the hit rate on the processing speed. The total number of tests is 10 times. See the code below for details.

result

While the list reference was slow enough to die, the dictionary and set type were super fast. In the list, it was found that the processing speed was clearly slower when the hit rate was low than when the hit rate was high. The unit is seconds, which is the total time when executed 10 times. On the other hand, the processing speed of the dictionary type and the set type did not decrease so much. (3/5 8:08 Corrected the figure because the range was incorrect) speed_in.png

Conclusion

In a list with a large number of elements Element in list Sentences should be avoided as much as possible. Really slow enough to die.

Experimental codes

def lists(q,h):
  ls = [i for i in range(0,q*h,h)]
  for i in range(q): i in ls

def dicts(q,h):
  dc = {i:i for i in range(0,q*h,h)}
  for i in range(q): i in dc  

def setts(q,h):
  st = set(i for i in range(0,q*h,h))
  for i in range(q): i in st

def exe(func,num=100):
  from timeit import timeit
  setup = 'from __main__ import ' + func[:5]
  print "%s: %s" % (func, timeit(func, setup, number=num))

if __name__=='__main__':
  q = 10**4
  funcs = ['lists','dicts','setts']
  hits = [1,2,3]
  for h in hits:
    for f in funcs:
      func = '%s(%s, %s)'  % (f,q,h)
      print func
      exe(func,10)

Recommended Posts

I checked the reference speed when using python list, dictionary, and set type in.
I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.
I want to make the Dictionary type in the List unique
I get a can't set attribute when using @property in python
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I set the environment variable with Docker and displayed it in Python
I measured the speed of list comprehension, for and while with python2.7.
I compared "python dictionary type" and "excel function"
[Python] Precautions when retrieving data by scraping and putting it in the list
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
To speed up python, summarize the amount of calculation of collection type (list / tuple / dictionary / set) for each purpose.
Initial settings when using the foursquare API in python
I checked out the versions of Blender and Python
Get the value of a specific key in a list from the dictionary type in the list with Python
I investigated the calculation time of "X in list" (linear search / binary search) and "X in set"
I checked the Python package pre-installed in Google Cloud Dataflow
Get the MIME type in Python and determine the file format
Sort and output the elements in the list as elements and multiples in Python.
Master the type in Python? (When should type check be done)
I tried programming the chi-square test in Python and Java.
Solve the Japanese problem when using the CSV module in Python.
I implemented N-Queen in various languages and measured the speed
Check the argument type annotation when executing a function in Python and make an error
I clustered the dollar yen using the k-medoids method in python and calculated the correct answer rate.
I compared the speed of Hash with Topaz, Ruby and Python
Difference between list () and [] in Python
Precautions when using pit in Python
I got an AttributeError when mocking the open method in python
Precautions when using a list or dictionary as the default argument
I compared Node.js and Python in creating thumbnails using AWS Lambda
Get and set the value of the dropdown menu using Python and Selenium
I wrote the queue in Python
What to do when the value type is ambiguous in Python?
When using regular expressions in Python
I wrote the stack in Python
I replaced the numerical calculation of Python with Rust and compared the speed
The file name was bad in Python and I was addicted to import
Notify using Notification Center when the execution environment is macOS in Python
A useful note when using Python for the first time in a while
Notes using cChardet and python3-chardet in Python 3.3.1.
Try using the Wunderlist API in Python
Try using the Kraken API in Python
OR the List in Python (zip function)
Tweet using the Twitter API in Python
I tried using Bayesian Optimization in Python
Difference between append and + = in Python list
Associate the table set in python models.py
Get the EDINET code list in Python
[Python] I want to know the variables in the function when an error occurs!
plot the coordinates of the processing (python) list and specify the number of times in draw ()
I compared the speed of go language web framework echo and python web framework flask
I stumbled on the character code when converting CSV to JSON in Python
What I did when I got stuck in the time limit with lambda python
Three things I was addicted to when using Python and MySQL with Docker
Note that I understand the least squares algorithm. And I wrote it in Python.
I want to get the file name, line number, and function name in Python 3.4
I checked the library for using the Gracenote API
[Python] Sort the list of pathlib.Path in natural sort
vprof --I tried using the profiler for Python
I tried web scraping using python and selenium
I tried object detection using Python and OpenCV