本文介绍了使用“open()”读取文件vs“与open()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多关于阅读python文件的文章和问题。但我仍然想知道是什么使python有多种方式来完成相同的任务。简单地说,我想知道的是,使用这两个方法的性能影响是什么?使用使用 语句不是为了提升性能,我不认为使用和语句相关的任何性能增益或损失,只要,您执行相同的清理活动,使用和语句自动执行。



当您使用 语句打开函数,您不需要在最后关闭文件,因为带有会自动关闭它。



另外,与语句不只是打开文件,与上下文管理器结合使用。基本上,如果你有一个对象,你想确保一旦你完成了它的清理或发生了某种错误,你可以将其定义为和 with 语句将调用它的 __ enter __() code>和 __ exit __()方法进入和退出with块。根据 -

另外,执行性能在进入和离开with语句正文时被调用。使用与进行测试,而不是使用它

  In [14] :def foo():
....:f = open('a.txt','r')
....:for l in f:
... 。:通过
....:f.close()
....:

在[15]中:def foo1():
.. ..:with open('a.txt','r')as f:
....:for l in f:
....:pass
... 。:

在[17]中:%timeit foo()
最慢的r un比最快的时间长了41.91倍。这可能意味着一个中间结果被缓存
10000循环,每个循环最好为3:186μs

在[18]中:%timeit foo1()
最慢的运行比最快的时间长了206.14倍。这可能意味着一个中间结果被缓存
10000循环,最好是3:每个循环179μs

在[19]中:%timeit foo()
最慢的运行比最快的时间长了202.51倍。这可能意味着一个中间结果被缓存
10000循环,每个循环最好是3:180μs

在[20]中:%timeit foo1()
10000循环,最好3:每循环193μs

在[21]中:%timeit foo1()
10000循环,最好是3:每循环194μs


I know there are plenty of articles and questions answered regarding reading files in python. But still I'm wondering what made python to have multiple ways to do the same task. Simply what I want to know is, what is the performance impact of using these two methods?

解决方案

Using with statement is not for performance gain, I do not think there are any performance gains or loss associated with using with statement, as long as, you perform the same cleanup activity that using with statement would perform automatically.

When you use with statement with open function, you do not need to close the file at the end, because with would automatically close it for you.

Also, with statement is not just for openning files, with is used in conjuction with context managers. Basically, if you have an object that you want to make sure it is cleaned once you are done with it or some kind of errors occur, you can define it as a context manager and with statement will call its __enter__() and __exit__() methods on entry to and exit from the with block. According to PEP 0343 -

Also, performance testing of using with and not using it -

In [14]: def foo():
   ....:     f = open('a.txt','r')
   ....:     for l in f:
   ....:         pass
   ....:     f.close()
   ....:

In [15]: def foo1():
   ....:     with open('a.txt','r') as f:
   ....:         for l in f:
   ....:             pass
   ....:

In [17]: %timeit foo()
The slowest run took 41.91 times longer than the fastest. This could mean that an intermediate result is being cached
10000 loops, best of 3: 186 µs per loop

In [18]: %timeit foo1()
The slowest run took 206.14 times longer than the fastest. This could mean that an intermediate result is being cached
10000 loops, best of 3: 179 µs per loop

In [19]: %timeit foo()
The slowest run took 202.51 times longer than the fastest. This could mean that an intermediate result is being cached
10000 loops, best of 3: 180 µs per loop

In [20]: %timeit foo1()
10000 loops, best of 3: 193 µs per loop

In [21]: %timeit foo1()
10000 loops, best of 3: 194 µs per loop

这篇关于使用“open()”读取文件vs“与open()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:42