3

Which runs faster: using OutputStreamWriter.write() for each string to be written, or using StringBuilder to create one large string, then using write() once? Please explain why.

Here's using write() many times:

writer.write("Registered Players:\n"); while (it.hasNext()) { int playerID = (Integer) it.next(); Player player = playerRegistry.get(playerID); writer.write(playerID+": "+player.getPlayerName()+"\n"); } 

And here's with StringBuilder:

builder.append("Registered Players:\n"); while (it.hasNext()) { int playerID = (Integer) it.next(); Player player = playerRegistry.get(playerID); builder.append(playerID+": "+player.getPlayerName()+"\n"); } writer.write(builder.toString()); 

3 Answers 3

4

It all depends on which kind of OutputStream you're writing to.

If it's writing to memory (ByteArrayOutputStream), it won't make any difference.

If you're writing to a BufferedOutputStream, the stream will buffer your lines in memory and write the buffer once it's full to the underlying stream, so it won't make any difference either.

If you're writing to a FileOutputStream or a SocketOutputStream, buffering will lead to better performance. But buffering everything into memory could be a bad idea if the data to write is too big: it could need too much memory.

The best thing to do is to use a buffered writer or stream, which will take care of buffering for you, transparently, and thus avoid too many low-level writes without having to buffer explicitely into a StringBuilder.

Sign up to request clarification or add additional context in comments.

1 Comment

After doing some more research, it looks like my OutputStreamWriter converts a character stream to a byte stream using a buffer. So there's probably very little difference between the two methods.For anyone else interested in speed, the Java docs says "For top efficiency, consider wrapping an OutputStreamWriter within a BufferedWriter so as to avoid frequent converter invocations. For example: Writer out = new BufferedWriter(new OutputStreamWriter(System.out));"
1

Depends what the output stream writer feeds, I'd say. If its a ByteArrayOutputStream, then I would think that the overhead of StringBuilder will slow it down. If however, the OutputStreamWriter feeds an unbuffered socket stream, then I would expect the StringBuilder to be faster, though bear in mind that with the StringBuilder you'll need enough memory to hold the whole of the data that you want to send in memory, so I wouldn't expect to scale as well.

As always, when it comes to questions of performance: write two prototypes and test it, because each scenario can result in unexpected or surprising results.

Comments

0

Since we are talking about android i think it would be best to not buffer your output. Using a stringbuilder will probably result in a greater memory usage and with calling StringBuilder.toString() the whole string again is copied and passed to the stream you are using (and maybe buffered again). So if you are handling a big amount of data this will cause the gc to clean after you and thats going to reduce performance too.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.