-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
2,735 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.