Skip to content

Commit

Permalink
Rename to Chain; add reverseIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Luka Jacobowitz committed Aug 12, 2018
1 parent 534bf3d commit 1dd44e4
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 187 deletions.
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
package cats.bench

import cats.data.Catenable
import fs2.{Catenable => OldCat}
import chain.Chain
import cats.data.Chain
import fs2.Catenable
import chain.{Chain => OldChain}
import org.openjdk.jmh.annotations.{Benchmark, Scope, State}

@State(Scope.Thread)
class CatenableBench {
class ChainBench {

private val smallChain = Chain(1, 2, 3, 4, 5)
private val smallCatenable = Catenable(1, 2, 3, 4, 5)
private val smallFs2Catenable = OldCat(1, 2, 3, 4, 5)
private val smallVector = Vector(1, 2, 3, 4, 5)
private val smallList = List(1, 2, 3, 4, 5)
private val smallChain = Chain(smallList)
private val smallOldChain = OldChain(smallList)

private val largeCatenable = (0 to 1000)
.foldLeft(Catenable.empty[Int])((acc, _) => acc ++ Catenable.fromSeq(0 to 1000))
private val largeFs2Catenable = OldCat.fromSeq(0 to 1000000)
private val largeChain = (0 to 1000)
.foldLeft(Chain.empty[Int])((acc, _) => acc ++ Chain.fromSeq(0 to 1000))
private val largeCatenable = Catenable.fromSeq(0 to 1000000)
private val largeVector = (0 to 1000000).toVector
private val largeList = (0 to 1000000).toList
private val largeChain = (0 to 1000).foldLeft(Chain.empty[Int])((acc, _) => acc ++ Chain(0 to 1000))
private val largeOldChain = (0 to 1000).foldLeft(OldChain.empty[Int])((acc, _) => acc ++ OldChain(0 to 1000))


@Benchmark def mapSmallChain: Chain[Int] = smallChain.map(_ + 1)
@Benchmark def mapSmallCatenable: Catenable[Int] = smallCatenable.map(_ + 1)
@Benchmark def mapSmallFs2Catenable: OldCat[Int] = smallFs2Catenable.map(_ + 1)
@Benchmark def mapSmallVector: Vector[Int] = smallVector.map(_ + 1)
@Benchmark def mapSmallList: List[Int] = smallList.map(_ + 1)
@Benchmark def mapSmallChain: Chain[Int] = smallChain.map(_ + 1)
@Benchmark def mapSmallOldChain: OldChain[Int] = smallOldChain.map(_ + 1)


@Benchmark def mapLargeChain: Chain[Int] = largeChain.map(_ + 1)
@Benchmark def mapLargeCatenable: Catenable[Int] = largeCatenable.map(_ + 1)
@Benchmark def mapLargeFs2Catenable: OldCat[Int] = largeFs2Catenable.map(_ + 1)
@Benchmark def mapLargeVector: Vector[Int] = largeVector.map(_ + 1)
@Benchmark def mapLargeList: List[Int] = largeList.map(_ + 1)
@Benchmark def mapLargeChain: Chain[Int] = largeChain.map(_ + 1)
@Benchmark def mapLargeOldChain: OldChain[Int] = largeOldChain.map(_ + 1)



@Benchmark def foldLeftSmallChain: Int = smallChain.foldLeft(0)(_ + _)
@Benchmark def foldLeftSmallCatenable: Int = smallCatenable.foldLeft(0)(_ + _)
@Benchmark def foldLeftSmallFs2Catenable: Int = smallFs2Catenable.foldLeft(0)(_ + _)
@Benchmark def foldLeftSmallVector: Int = smallVector.foldLeft(0)(_ + _)
@Benchmark def foldLeftSmallList: Int = smallList.foldLeft(0)(_ + _)
@Benchmark def foldLeftSmallChain: Int = smallChain.foldLeft(0)(_ + _)
@Benchmark def foldLeftSmallOldChain: Int = smallOldChain.foldLeft(0)(_ + _)


@Benchmark def foldLeftLargeChain: Int = largeChain.foldLeft(0)(_ + _)
@Benchmark def foldLeftLargeCatenable: Int = largeCatenable.foldLeft(0)(_ + _)
@Benchmark def foldLeftLargeFs2Catenable: Int = largeFs2Catenable.foldLeft(0)(_ + _)
@Benchmark def foldLeftLargeVector: Int = largeVector.foldLeft(0)(_ + _)
@Benchmark def foldLeftLargeList: Int = largeList.foldLeft(0)(_ + _)
@Benchmark def foldLeftLargeChain: Int = largeChain.foldLeft(0)(_ + _)
@Benchmark def foldLeftLargeOldChain: Int = largeOldChain.foldLeft(0)(_ + _)




@Benchmark def consSmallChain: Chain[Int] = 0 +: smallChain
@Benchmark def consSmallCatenable: Catenable[Int] = 0 +: smallCatenable
@Benchmark def consSmallFs2Catenable: OldCat[Int] = 0 +: smallFs2Catenable
@Benchmark def consSmallVector: Vector[Int] = 0 +: smallVector
@Benchmark def consSmallList: List[Int] = 0 +: smallList
@Benchmark def consSmallChain: Chain[Int] = 0 +: smallChain
@Benchmark def consSmallOldChain: OldChain[Int] = 0 +: smallOldChain

@Benchmark def consLargeChain: Chain[Int] = 0 +: largeChain
@Benchmark def consLargeCatenable: Catenable[Int] = 0 +: largeCatenable
@Benchmark def consLargeFs2Catenable: OldCat[Int] = 0 +: largeFs2Catenable
@Benchmark def consLargeVector: Vector[Int] = 0 +: largeVector
@Benchmark def consLargeList: List[Int] = 0 +: largeList
@Benchmark def consLargeChain: Chain[Int] = 0 +: largeChain
@Benchmark def consLargeOldChain: OldChain[Int] = 0 +: largeOldChain

@Benchmark def createTinyChain: Chain[Int] = Chain(1)
@Benchmark def createTinyCatenable: Catenable[Int] = Catenable(1)
@Benchmark def createTinyFs2Catenable: OldCat[Int] = OldCat(1)
@Benchmark def createTinyVector: Vector[Int] = Vector(1)
@Benchmark def createTinyList: List[Int] = List(1)
@Benchmark def createTinyChain: Chain[Int] = Chain.single(1)
@Benchmark def createTinyOldChain: OldChain[Int] = OldChain.single(1)

@Benchmark def createSmallChain: Chain[Int] = Chain(1, 2, 3, 4, 5)
@Benchmark def createSmallCatenable: Catenable[Int] = Catenable(1, 2, 3, 4, 5)
@Benchmark def createSmallFs2Catenable: OldCat[Int] = OldCat(1, 2, 3, 4, 5)
@Benchmark def createSmallVector: Vector[Int] = Vector(1, 2, 3, 4, 5)
@Benchmark def createSmallList: List[Int] = List(1, 2, 3, 4, 5)
@Benchmark def createSmallChain: Chain[Int] = Chain(Seq(1, 2, 3, 4, 5))
@Benchmark def createSmallOldChain: OldChain[Int] = OldChain(Seq(1, 2, 3, 4, 5))
}
14 changes: 7 additions & 7 deletions bench/src/main/scala/cats/bench/CollectionMonoidBench.scala
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package cats.bench

import cats.Monoid
import cats.data.Catenable
import cats.data.Chain
import cats.implicits._
import chain.Chain
import chain.{Chain => OldChain}
import org.openjdk.jmh.annotations.{Benchmark, Scope, State}

@State(Scope.Thread)
class CollectionMonoidBench {

private val largeList = (0 to 1000000).toList

implicit def monoidChain[A]: Monoid[Chain[A]] = new Monoid[Chain[A]] {
def empty: Chain[A] = Chain.empty[A]
implicit def monoidOldChain[A]: Monoid[OldChain[A]] = new Monoid[OldChain[A]] {
def empty: OldChain[A] = OldChain.empty[A]

def combine(x: Chain[A], y: Chain[A]): Chain[A] = x ++ y
def combine(x: OldChain[A], y: OldChain[A]): OldChain[A] = x ++ y
}

@Benchmark def accumulateCatenable: Catenable[Int] = largeList.foldMap(Catenable.singleton)
@Benchmark def accumulateChain: Chain[Int] = largeList.foldMap(Chain.one)
@Benchmark def accumulateVector: Vector[Int] = largeList.foldMap(Vector(_))
@Benchmark def accumulateList: List[Int] = largeList.foldMap(List(_))
@Benchmark def accumulateChain: Chain[Int] = largeList.foldMap(Chain.single)
@Benchmark def accumulateOldChain: OldChain[Int] = largeList.foldMap(OldChain.single)
}
Loading

0 comments on commit 1dd44e4

Please sign in to comment.