Python StringIO Examples and Benchmark

Use the StringIO class from the io namespace. StringIO writes to strings like files.

StringIO writes to strings. It uses a syntax similar to that for files. We can use print method calls, or invoke the write method. StringIO has performance advantages for creating large strings.Strings

We first import the io namespace. We create a new stream by calling io.StringIO: this returns a stream variable. Next, in a for-loop, I write some data to our stream. This is just for the example.

Write: We call the write method to output data to our stream. It is just stored in memory. A print() call also works.

Getvalue: The getvalue method returns a string containing the data written to the in-memory buffer.

Close: We call the close method to prevent further writes to our StringIO stream. This is good housekeeping.

Python program that uses StringIO import io out = io.StringIO() # Print these string values in a loop. for i in range(0, 100): out.write("Value = ") out.write(str(i)) out.write(" ") # Get string and display first 20 character. data = out.getvalue() print(data[0:20]) out.close() Output Value = 0 Value = 1

Print. We do not need to use write() to write with StringIO. We can instead use print() and specify the optional "file" argument. The print method is powerful: it can target any compatible IO stream.Print
Python program that uses print import io out = io.StringIO() # Print to StringIO stream, no end char. print("Hello", file=out, end="") # Print string contents to console. print(out.getvalue()) Output Hello

Performance. Is StringIO faster than a series of string appends? I tested a program that wrote large strings, appending strings many times. I found StringIO was faster, in two Python implementations.

PyPy: In the PyPy implementation, StringIO was nearly twice as fast as a string append.

Note: The if-check in the benchmark loops is just a simple sanity check. The strings must begin with the letter V.

Python program that benchmarks StringIO, string concats import io import time print(time.time()) # 1. Use StringIO. for x in range(0, 10000): out = io.StringIO() for i in range(0, 100): out.write("Value = ") out.write(str(i)) out.write(" ") # Get string. contents = out.getvalue() out.close() # Test first letter. if contents[0] != 'V': raise Error print(time.time()) # 2. Use string appends. for x in range(0, 10000): data = "" for i in range(0, 100): data += "Value = " data += str(i) data += " " # Test first letter. if data[0] != 'V': raise Error print(time.time()) Output 1404346870.027 [PyPy3 2.3.1] 1404346870.286 StringIO: 0.259 s 1404346870.773 Concat: 0.487 s 1404346852.222672 [Python 3.3] 1404346853.343738 StringIO: 1.121 s 1404346854.814824 Concat: 1.471 s

Summary. Many ways exist to append strings together. StringIO is faster than using direct string concats on large strings. It introduces syntactic complexity, though: strings alone are simpler.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to