File open function in Python3 (difference between open and codecs.open and speed comparison)

First conclusion

The processing speed is faster with the open function. The versatile docecs.open function. Both the readability and simplicity of the source code. In short, it was up to you to use it.

Trigger

When you are doing "100 language processing knocks" that you all know for practicing python3, I noticed that there are two types of functions that open files (open and codecs.open). Both seem to be often used.

What is the difference between open and codecs.open?

The codecs.open function can open a file encoded in Python2 series. Yeah, maybe it's convenient. I understand that this is the difference in usage (please let me know if you point out).

However, there are some differences in how to write the source code. Let's take a look below.

Source code comparison

Let's create a code that reads hoge.txt like the one below. hoge.txt

open source code

open_code Execution result ↓ open_result

The open function is included in the standard library, so there is no need for special import. The number of lines will be shorter by that amount, but it seems to be unfashionable because the argument name must be specified.

Source code for codecs.open

codecs.open_code Execution result ↓ codecs.open_result

You need to import the codecs library. However, one line is short and looks smart because no argument name is specified.

Comparison of execution speed

Overwrite 1 million times and compare the speed. The execution was performed 5 times each.

Comparison program source code

result

final_result

The above measurement results are available. The open function was about 3 times faster. It's like a red comet.

Conclusion (I wrote the same thing as the conclusion at the beginning)

The processing speed is faster with the open function. The versatile docecs.open function. Both the readability and simplicity of the source code. In short, it was up to you to use it.

Recommended Posts

File open function in Python3 (difference between open and codecs.open and speed comparison)
Difference between list () and [] in Python
Difference between == and is in python
[Python] Difference between function and method
difference between statements (statements) and expressions (expressions) in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[python] Difference between variables and self. Variables in class
About the difference between "==" and "is" in python
Difference between Ruby and Python in terms of variables
Difference between return, return None, and no return description in Python
Python module num2words Difference in behavior between English and Russian
List concatenation method in python, difference between list.extend () and “+” operator
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between python2 series and python3 series dict.keys ()
Speed comparison between CPython and PyPy
Function synthesis and application in Python
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
Difference in how to write if statement between ruby ​​and python
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
Differences in syntax between Python and Java
Difference between PHP and Python finally and exit
Write O_SYNC file in C and Python
[Python] Difference between class method and static method
Exclusive file access between processes in Python
Difference between docker-compose env_file and .env file
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
Open an Excel file in Python and color the map of Japan
File DL, byte value and delete in Python3
Create new file [Comparison between Bash and PowerShell]
Speed evaluation of CSV file output in Python
Speed comparison between incrementing count variable and enumerate
Difference in writing method to read external source code between Ruby and Python
File operations in Python
File processing in Python
[python] Calculation of months and years of difference in datetime
File operations in Python
[Ubuntu] [Python] Face detection comparison between dlib and OpenCV
Mutual conversion between JSON and YAML / TOML in Python
[Python] Explain the difference between strftime and strptime in the datetime module with an example
Install OpenCV 3 (core + contrib) in Windows & Python 3 environment & Difference between OpenCV 2 and OpenCV 3 & Easy operation check
Compare "relationship between log and infinity" in Gauche (0.9.4) and Python (3.5.1)
[Python] Open the csv file in the folder specified by pandas
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
Get the MIME type in Python and determine the file format
[Python] How to set variable names dynamically and speed comparison
Comparison of how to use higher-order functions in Python 2 and 3
Get the current date and time in Python, considering the time difference
AtCoder ABC151 Problem D Speed comparison in C ++ / Python / PyPy
Difference between process and job
Create a function in Python
Download the file in Python
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Use callback function in Python