StringIO
This Python type 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. In some implementations, programs may run twice as fast.
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.
print()
call also works.string
containing the data written to the in-memory buffer.StringIO
stream. This is good housekeeping.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()Value = 0 Value = 1
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 can target any compatible IO stream.
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())Hello
Is StringIO
faster than a series of string
appends? I tested a program that wrote large strings, appending strings many times.
StringIO
was faster, in two Python implementations. In PyPy, StringIO
was nearly twice as fast as a string
append.if
-check in the benchmark loops is just a simple sanity check. The strings must begin with the letter V.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())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
Many ways exist to append strings together. StringIO
is faster than using direct string
concats on large strings. It has more complex syntax than string
appends.