pythonstringiocstringio

Why is StringIO object slower than real file object?


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).
  2. There's also a much faster implementation in C, called cStringIO, but it's not subclassable.

StringIO just like a memory file object, why is it slower than real file object?


Solution

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