为什么StringIO对象比真实文件对象慢? [英] Why is StringIO object slower than real file object?

查看:82
本文介绍了为什么StringIO对象比真实文件对象慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看StringIO的来源,上面写着一些注释:

I'm looking through the source of StringIO where it says says some notes:

  1. 使用真实文件通常更快(但不太方便).
  2. 在C中还有一个更快的实现,称为cStringIO,但是 它不是子类的.
  1. Using a real file is often faster (but less convenient).
  2. There's also a much faster implementation in C, called cStringIO, but it'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对象比真实文件对象慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆