Permalink
Browse files

Add simple benchmark for utf8Decode

  • Loading branch information...
fthomas committed Feb 12, 2014
1 parent cc14dc5 commit e9b01b6484cdf777fa27c46537947a3c33b6abac
Showing with 2,735 additions and 0 deletions.
  1. +68 −0 src/test/scala/scalaz/stream/examples/ReadingUTF8.scala
  2. +2,667 −0 testdata/utf8.txt
@@ -0,0 +1,68 @@
package scalaz.stream
import java.io._
import org.scalacheck._
import Prop._
import process1._
import scalaz.concurrent.Task
object ReadingUTF8 extends Properties("examples.ReadingUTF8") {
def time[A](a: => A): (A, Double) = {
val now = System.nanoTime
val result = a
val millis = (System.nanoTime - now) / 1e6
(result, millis)
}
def benchmark[A](descr: String, n: Int, a: => A) = {
(0 to n).foreach { _ =>
val (result, millis) = time(a)
println(s"$descr:\t$millis\t$result")
}
true
}
val bufferSize = 4096
//val bufferSize = 8192
val testFile = "testdata/utf8.txt"
val scalazIo: Task[Option[Long]] =
Process.constant(bufferSize)
.through(io.fileChunkR(testFile, bufferSize))
.map(a => Bytes.unsafe(a))
.pipe(utf8Decode)
.map(_.map(_.toLong).sum)
.reduce(_ + _)
.runLast
def javaIo = {
val fileStream = new FileInputStream(testFile)
val inputStream = new InputStreamReader(fileStream, "UTF-8")
val bufferedReader = new BufferedReader(inputStream, bufferSize)
val array = Array.ofDim[Char](bufferSize)
def readChars(a: Array[Char]) = bufferedReader.read(a, 0, bufferSize)
var l = 0
var sum: Long = 0
var count = readChars(array)
while (count >= 0) {
l += 1
val s = new String(array, 0, count)
sum += s.map(_.toLong).sum
count = readChars(array)
}
bufferedReader.close()
inputStream.close()
fileStream.close()
Some(sum)
}
property("benchmark") = secure {
benchmark("scalaz", 10, scalazIo.run)
benchmark("Java", 10, javaIo)
}
}
Oops, something went wrong.

0 comments on commit e9b01b6

Please sign in to comment.