Created
August 14, 2012 21:50
-
-
Save hoffrocket/3353347 to your computer and use it in GitHub Desktop.
WriteBufferTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream | |
import java.io.OutputStreamWriter | |
object WriteBufferTest { | |
def writeData(appendable: java.lang.Appendable) { | |
var i = 0 | |
while (i < 1000) { | |
appendable.append("hello there ").append(i.toString()) | |
i += 1 | |
} | |
} | |
def time[T](count: Int)(f: => T): Long = { | |
val start = System.nanoTime | |
var i = 0 | |
while (i < count) { | |
val r = f | |
i +=1 | |
} | |
((System.nanoTime - start)) | |
} | |
def main(args: Array[String]) { | |
val loopCount = 10000 | |
println(time(loopCount)({ | |
outputStreamWrite | |
})) | |
println(time(loopCount)({ | |
stringBuilderWrite | |
})) | |
println(time(loopCount)({ | |
outputStreamWrite | |
})) | |
println(time(loopCount)({ | |
stringBuilderWrite | |
})) | |
} | |
private def outputStreamWrite: Array[Byte] = { | |
val baos = new ByteArrayOutputStream(4096) | |
val osw = new OutputStreamWriter(baos, "UTF-8") | |
writeData(osw) | |
baos.toByteArray() | |
} | |
private def stringBuilderWrite: java.lang.String = { | |
val builder = new java.lang.StringBuilder | |
writeData(builder) | |
builder.toString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment