Skip to content

Instantly share code, notes, and snippets.

@dwhitney
Last active November 7, 2016 15:26
Show Gist options
  • Save dwhitney/2468f41be68056ebf501f4642fb7462a to your computer and use it in GitHub Desktop.
Save dwhitney/2468f41be68056ebf501f4642fb7462a to your computer and use it in GitHub Desktop.
/*
Results
[info] Running StreamsExperiments
[info] Count: 923062246
[info] Time: 5301
[info] Count: 923062246
[info] Time: 4588
[info] Count: 923062246
[info] Time: 4460
[info] Count: 923062246
[info] Time: 4597
[info] Count: 923062246
[info] Time: 4554
[info] Count: 923062246
[info] Time: 4582
[info] Count: 923062246
[info] Time: 4232
[info] Count: 923062246
[info] Time: 4031
[info] Count: 923062246
[info] Time: 4013
[info] Count: 923062246
[info] Time: 3961
[info]
[info]
[info] ... scala.io.Source
[info] Count: 923062246
[info] Time: 1884
[info] Count: 923062246
[info] Time: 1696
[info] Count: 923062246
[info] Time: 1792
[info] Count: 923062246
[info] Time: 1685
[info] Count: 923062246
[info] Time: 1681
[info] Count: 923062246
[info] Time: 1681
[info] Count: 923062246
[info] Time: 1679
[info] Count: 923062246
[info] Time: 1698
[info] Count: 923062246
[info] Time: 1694
[info] Count: 923062246
[info] Time: 1683
*/
import fs2.{io, Stream, text, Task}
import java.io._
import java.nio._
import java.nio.channels._
import java.nio.file._
import java.nio.charset._
import java.util.Scanner
import java.util.stream.{Stream => JStream}
import scala.io.Source
object StreamsExperiments extends App{
val decoder = Charset.forName("UTF-8").newDecoder();
var i = 0
while(i < 10){
val begin = System.currentTimeMillis
val charCount: Task[Unit] =
io.file.readAll[Task](Paths.get("test-1GB.txt"), 1000000)
.chunkLimit(1000000)
.map{ chunk =>
decoder.decode(
ByteBuffer.wrap(chunk.toArray)
).toString.split("\n")
}
.flatMap{ array => Stream.emits(array)}
.fold(0L)(_ + _.length)
.map(count => println(s"Count: $count"))
.run
charCount.unsafeRun
println(s"Time: ${System.currentTimeMillis - begin}")
i = i + 1
}
println("\n\n... scala.io.Source")
i = 0
while(i < 10){
val begin = System.currentTimeMillis
val count = Source.fromFile("test-1GB.txt")
.getLines
.foldLeft(0L)(_ + _.length)
println(s"Count: $count")
println(s"Time: ${System.currentTimeMillis - begin}")
i = i + 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment