问题描述
我正在查看StringIO
的来源,上面写着一些注释:
I'm looking through the source of StringIO
where it says says some notes:
- 使用真实文件通常更快(但不太方便).
- 在C中还有一个更快的实现,称为
cStringIO
,但是它不是子类的.
- Using a real file is often faster (but less convenient).
- There's also a much faster implementation in C, called
cStringIO
, butit's not subclassable.
StringIO
就像一个内存文件对象一样,为什么它比真实文件对象慢?
StringIO
just like a memory file object,why is it slower than real file object?
推荐答案
Python的文件处理完全在C中实现.这意味着它相当快(至少在与本机C代码相同的数量级上).
Python's file handling is implemented entirely in C. This means that it's quite fast (at least in the same order of magnitude as native C code).
但是,StringIO库是用Python编写的.这样就解释了模块本身,并附带了相关的性能损失.
The StringIO library, however, is written in Python. The module itself is thus interpreted, with the associated performance penalties.
您知道,还有另一个模块cStringIO,具有相似的接口,您可以在对性能敏感的代码中使用.不能归类的原因是 ,因为它是用C语言编写的.
As you know, there is another module, cStringIO, with a similar interface, which you can use in performance-sensitive code.The reason this isn't subclassable is because it's written in C.
这篇关于为什么StringIO对象比真实文件对象慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!