[Python] How to set variable names dynamically and speed comparison

Postscript

It was pointed out that locals () cannot be used normally in a function (it is even stated in the official documentation that it should not be used in the first place). Be careful when using it.

Introduction

I don't think there are many, but there are times when you want to set variable names dynamically, and sometimes you don't, so I tried to find out how to do that. (For example, if you want to read data from an external file and use the file name as a variable name?)

I found 2 (+ α) ways, so I would like to compare the speeds as an introduction.

Speed comparison rules and preparation

rule -Measure using the %% timeit command on the Jupyter Notebook. -Use 5 random lowercase letters as the variable name. -Substitute a list containing integers from 0 to 9 as values. -Measure the average of 1,000 loops.

** Preparation ** Prepare variable names in advance so as not to affect the measurement.


import random

chrs = [chr(i) for i in range(ord('a'), ord('z')+1)]
names = [''.join(random.sample(chrs, 10)) for i in range(10)]

Method 1 Use ʻexec ()`

The most common method.

%%timeit

for i in range(1000):
    exec(f'{names[i]} = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]')

result 20.7 ms ± 75.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

Method 2 Use locals () or globals ()

locals () and globals () are recognized as local variables and dictionaries that manage global variables, respectively (I'm sorry if they are different). As with the normal dictionary type, you can use the indexer and .get () to get the value, and use .update () to update.

%%timeit
for i in range(1000):
    locals().update({f'{names[i]}':[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]})

result 386 µs ± 24.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Change the method a little

%%timeit
for i in range(1000):
    locals()[names[i]] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

result 248 µs ± 387 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Summary

ʻExec ()ratio, ・ ʻExec (): 1x ・ Locals (). update (): ** 53 times ** ・ Locals () []: *** 83 times *** I was able to define it as quickly as possible.

** Conclusion **

`locals () (or globals ())` is fast.

Recommended Posts

[Python] How to set variable names dynamically and speed comparison
Comparison of how to use higher-order functions in Python 2 and 3
How to speed up Python calculations
How to package and distribute Python scripts
How to dynamically define variables in Python
How to install and use pandas_datareader [Python]
python: How to use locals () and globals ()
How to dynamically zero pad in Python
[Python] How to calculate MAE and RMSE
How to use Python zip and enumerate
How to use is and == in Python
How to set proxy, redirect and SSL authentication for Python Requests module
How to generate permutations in Python and C ++
How to embed a variable in a python string
[Python] How to read data from CIFAR-10 and CIFAR-100
How to convert SVG to PDF and PNG [Python]
[Python] How to use hash function and tuple.
How to plot autocorrelation and partial autocorrelation in python
Speed comparison between incrementing count variable and enumerate
How to install Python
How to install python
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
How to get the variable name itself in python
[Python] [Django] How to use ChoiceField and how to add options
How to set up a Python environment using pyenv
File write speed comparison experiment between python 2.7.9 and pypy 2.5.0
[Python] How to sort dict in list and instance in list
[Blender] How to dynamically set the selection of EnumProperty
[Python] How to split and modularize files (simple, example)
[Python] How to create Correlation Matrix and Heat Map
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Work efficiency] How to change file names in Python
Python # How to check type and type for super beginners
How to set up and compile your Cython environment
What to use for Python stacks and queues (speed comparison of each data structure)
[2020.8 latest] How to install Python
How to install Python [Windows]
python3: How to use bottle (2)
Python, Java, C ++ speed comparison
How to swap elements in an array in Python, and how to reverse an array.
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
How to resolve variable scope conflicts between Jinja2 and AngularJS
How to use Python argparse
How to learn TensorFlow for liberal arts and Python beginners
[Python] Summary of how to use split and join functions
Python: How to use pydub
How to do Bulk Update with PyMySQL and notes [Python]
[Python] How to use checkio
How to convert Youtube to mp3 and download it super-safely [Python]
How to run Notepad ++ Python
How to scrape at speed per second with Python Selenium
How to change Python version
How to write a metaclass that supports both python2 and python3
How to develop in Python
Python 3 sorted and comparison functions
[python] How to judge scalar
How to execute external shell scripts and commands in python
[Python] How to use input ()