I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.

Introduction

Last time, it was confirmed in an experiment that the reference of the element in list is very slow compared to the dictionary and set type. http://qiita.com/cof/items/05f6ffc6d4e5b062aaa9 However, dictionary comprehensions should be fairly fast. I wondered if it would be explosive if I made a dictionary comprehension every time before referring to it, so I looked it up.

Execution environment

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

Execution condition

I checked it by referring to 10000 elements of the following two codes and measuring the time when it was executed 10 times. Number of elements 10000 Element in list Element in {Element: True for Element in List}

Result (total time when running 10 times)

Element in list 10.226 Element in {Element: True for Element in List} 124.887

Conclusion

It wasn't fast to make a dictionary comprehension every time.

Experimental codes

def in_list(n):
  ls = [i for i in range(n)]
  for i in range(n): i in ls
  
def in_dict(n):
  ls = [i for i in range(n)]
  for i in range(n): i in {i:True for i in ls}

def exe(func,num=100):
  from timeit import timeit
  setup = 'from __main__ import ' + func.split('(')[0]
  print "%s: %s" % (func, timeit(func, setup, number=num))
if __name__=='__main__':
  q = 10**4
  exe('in_list(%d)'%q,10)
  exe('in_dict(%d)'%q,10)

Recommended Posts

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 checked the reference speed when using python list, dictionary, and set type in.
I measured the speed of list comprehension, for and while with python2.7.
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
I compared the speed of Hash with Topaz, Ruby and Python
I replaced the numerical calculation of Python with Rust and compared the speed
I compared the speed of go language web framework echo and python web framework flask
Get the value of a specific key in a list from the dictionary type in the list with Python
Python: Create a dictionary from a list of keys and values
[Introduction to Python] I compared the naming conventions of C # and Python.
I made a program to check the size of a file in Python
[Python] Sort the list of pathlib.Path in natural sort
Make a copy of the list in Python
I compared "python dictionary type" and "excel function"
Compare the speed of Python append and map
plot the coordinates of the processing (python) list and specify the number of times in draw ()
Python list comprehension speed
[python] Get the list of classes defined in the module
I checked out the versions of Blender and Python
[Python] Outputs all combinations of elements in the list
Python3 comprehension (List, dictionary) that I have seen somewhere
The concept of reference in Python collapsed for a moment, so I experimented a little.
I investigated the calculation time of "X in list" (linear search / binary search) and "X in set"
I tried to compare the processing speed with dplyr of R and pandas of Python
[Python] I tried to summarize the array, dictionary generation method, loop method, list comprehension notation
I compared Java and Python!
I tried the accuracy of three Stirling's approximations in python
Applied practice of try/except and dictionary editing and retrieval in Python
Sort and output the elements in the list as elements and multiples in Python.
Get the number of specific elements in a python list
Reference order of class variables and instance variables in "self. Class variables" in Python
[Python] I installed the game from pip and played it
I tried programming the chi-square test in Python and Java.
I want to know the features of Python and pip
I want to make the Dictionary type in the List unique
[Tips] Problems and solutions in the development of python + kivy
What beginners learned from the basics of variables in python
I implemented N-Queen in various languages and measured the speed
Get the value of a specific key up to the specified index in the dictionary list in Python
Count the number of Thai and Arabic characters well in Python
Difference between list () and [] in Python
I made a Line bot that guesses the gender and age of a person from an image
Existence from the viewpoint of Python
[Python] I thoroughly explained the theory and implementation of logistic regression
I wrote the code to write the code of Brainf * ck in python
How to check the memory size of a dictionary in Python
[Python] I thoroughly explained the theory and implementation of decision trees
[python] Get the rank of the values in List in ascending / descending order
I compared Node.js and Python in creating thumbnails using AWS Lambda
I wrote the queue in Python
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
List of Linear Programming (LP) solvers and modelers available in Python
List of disaster dispatches from the Sapporo City Fire Department [Python]
About the basics list of Python basics
Get the title and delivery date of Yahoo! News in Python
I wrote the stack in Python
Reputation of Python books and reference books
Note that I understand the algorithm of the machine learning naive Bayes classifier. And I wrote it in Python.
Set up a dummy SMTP server in Python and check the operation of sending from Action Mailer
[Python] I analyzed the diary of a first-year member of society and made a positive / negative judgment on the life of a member of society.
I compared the performance of Vaex, Dask, and Pandas in CSV, Parquet, and HDF5 formats (for single files).