Why is StringIO object slower than real file object?

后端 未结 3 967
无人共我
无人共我 2021-01-12 11:36

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

  1. Using a real file is often faster (but less convenient)
3条回答
  •  死守一世寂寞
    2021-01-12 11:42

    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).

    The StringIO library, however, is written in Python. The module itself is thus interpreted, with the associated performance penalties.

    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.

提交回复
热议问题