Iterator Benchmark

Code to Run

def test(collectionSize: Int) {
  println("\"List (size " + collectionSize + "), test results:\"\n")

  import scalqa.Stream.Enable._

  class Foo(val id: Int) { def even = id % 2 == 0 }
  class Bar(val foo: Foo)

  val list: List[Bar] = (1 to collectionSize).map(i => new Bar(new Foo(i))).toList

  scalqa.Util.Benchmark(
    ("List as Iterator ", () => list.toIterator.filter(_.foo.even).flatMap(_ => list).map(_.foo).count(_.even)),
    ("List as Stream   ", () => list.all       .filter(_.foo.even).flatMap(_ => list).map(_.foo).count(_.even)))
}

Results to Expect

"List (size 10), test results:"

Num Name              Ops/Sec %   Memory %   Control Value Avg
--- ----------------- ------- --- ------ --- -----------------
1   List as Iterator  1.0m    71  185    56  25
2   List as Stream    1.5m    100 330    100 25

"List (size 100), test results:"

Num Name              Ops/Sec %   Memory % Control Value Avg
--- ----------------- ------- --- ------ - -----------------
1   List as Iterator  17.1k   36  0      0 2500
2   List as Stream    46.6k   100 0      0 2500

"List (size 1000), test results:"

Num Name              Ops/Sec %   Memory % Control Value Avg
--- ----------------- ------- --- ------ - -----------------
1   List as Iterator  163     45  0      0 250000
2   List as Stream    357     100 0      0 250000

Specialized Test

Code to Run

def specializedTest(collectionSize: Int) {
  println("\"List (size " + collectionSize + "), specialized test results:\"\n")

  import scalqa.Stream.Enable._

  val list: List[Int] = (1 to collectionSize).toList

  scalqa.Util.Benchmark(
    ("List as Iterator", () => list.toIterator.filter(_ % 2 == 0).flatMap(_ => list).map(_ / 2L).sum),
    ("List as Stream  ", () => list.all       .filter(_ % 2 == 0).flatMap(_ => list).map(_ / 2L).sum))
}

Results to Expect

"List (size 10), specialized test results:"

Num Name             Ops/Sec %   Memory %   Control Value Avg
--- ---------------- ------- --- ------ --- -----------------
1   List as Iterator 642.1k  57  0      0   125
2   List as Stream   1.1m    100 423    100 125

"List (size 100), specialized test results:"

Num Name             Ops/Sec %   Memory %   Control Value Avg
--- ---------------- ------- --- ------ --- -----------------
1   List as Iterator 7.2k    26  108.7k 100 125000
2   List as Stream   27.0k   100 545    0   125000

"List (size 1000), specialized test results:"

Num Name             Ops/Sec %   Memory %   Control Value Avg
--- ---------------- ------- --- ------ --- -----------------
1   List as Iterator 69      27  457.7k 100 125000000
2   List as Stream   255     100 10.0k  2   125000000