Skip to content

Instantly share code, notes, and snippets.

@etorreborre
Last active August 29, 2015 14:10
Show Gist options
  • Save etorreborre/a098180ccb7a62810b23 to your computer and use it in GitHub Desktop.
Save etorreborre/a098180ccb7a62810b23 to your computer and use it in GitHub Desktop.
Scalaz-stream nio vs io
/***
* THIS IS COMPLETELY WRONG!!!
* SEE HERE: https://groups.google.com/d/msg/scalaz/SuL7eTS_oW0/3cUvISJ5pGEJ
*/
import java.io._
import java.nio.file.{Path, Files}
import org.specs2.io._
import org.specs2.Specification
import scalaz._, Scalaz._, stream._
import scalaz.stream.nio._
class ScalazStreamSpec extends Specification { def is = s2"""
prepare ${step(createFile)}
with io $ioProcess
with nio $nioProcess
"""
def nioProcess = {
val p =
stream.io.linesR(bigPath.path).map(transform).intersperse("\n") |> stream.text.utf8Encode to stream.io.fileChunkW(("target" / "test" / "io.txt").path)
p.run.run
ok
}
def ioProcess = {
val p =
file.linesR(bigPath.path).map(transform).intersperse("\n") |> stream.text.utf8Encode to file.chunkW(("target" / "test" / "nio.txt").path)
p.run.run
ok
}
def transform(line: String): String = {
val fields = line.split("\\|")
line+"|"+fields(3).toInt
}
def createFile = if (!new File(bigPath.path).exists) {
val printer = new PrintWriter(bigPath.path)
val line = "1|2|3|4|5"
val lines = List.fill(10000000)(line)
lines.foreach(printer.write)
printer.close()
}
val bigPath = "target" / "test" / "big.txt"
}
sbt> ~test-only *ScalazStreamSpec* -- showtimes sequential
[info] ScalazStreamSpec
[info] prepare
[info] + with io (8 seconds, 703 ms)
[info] + with nio (68 ms)
[info]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment