Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Add lazyZip operation (formerly zipWith) #223

Merged
merged 1 commit into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ For more information, see the [CONTRIBUTING](CONTRIBUTING.md) file.
- [x] `sliding`
- [x] `unzip`
- [x] `values` / `valuesIterator`
- [x] `zip` / `zipWithIndex`
- [x] `zip` / `zipWithIndex` / `lazyZip`

### In-place mutating operations

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ class HashSetBenchmark {
}
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Long, b: Long) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = bh.consume(xs.lazyZip(xs).map((_, _)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add more benchmarks to compare xs.lazyZip(ys).map(f) with xs.zip(ys).map(f.tupled)?


@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any recommendation for when (not) to add new benchmarks to the project? These are going to add several hours to the running time

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No recommendation so far. New benchmarks are always welcomed for now. In my case anyway I only run a subset of the benchmarks when I run them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, what I do sometimes is that I group related benchmarks together so that it’s simpler to run just the benchmarks that I’m interested in: https://github.com/scala/collection-strawman/blob/master/benchmarks/time/src/main/scala/strawman/collection/GroupMapBenchmarks.scala


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,21 @@ class ImmutableArrayBenchmark {
}
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Long, b: Long) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = bh.consume(xs.lazyZip(xs).map((_, _)))

@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,21 @@ class LazyListBenchmark {
}
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Long, b: Long) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = bh.consume(xs.lazyZip(xs).map((_, _)))

@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,21 @@ class ListBenchmark {
}
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Long, b: Long) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = bh.consume(xs.lazyZip(xs).map((_, _)))

@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ class NumericRangeBenchmark {
var xs: NumericRange[Int] = _
var zs: NumericRange[Int] = _
var randomIndices: scala.Array[Int] = _
var zipped: IndexedSeq[(Long, Long)] = _
def fresh(n: Int) = NumericRange.inclusive(1, n, 1)

@Setup(Level.Trial)
def initTrial(): Unit = {
xs = fresh(size)
zs = NumericRange.inclusive(-1, (-size / 1000) min -2, -1)
zipped = xs.map(x => (x, x))
if (size > 0) {
randomIndices = scala.Array.fill(1000)(scala.util.Random.nextInt(size))
}
Expand Down Expand Up @@ -176,4 +178,25 @@ class NumericRangeBenchmark {
val result = xs.groupBy(_ % 5)
bh.consume(result)
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Int, b: Int) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = {
val xs1: IndexedSeq[Int] = xs
bh.consume(xs1.lazyZip(xs1).map((_, _)))
}

@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ class RangeBenchmark {
var xs: Range = _
var zs: Range = _
var randomIndices: scala.Array[Int] = _
var zipped: IndexedSeq[(Long, Long)] = _
def fresh(n: Int) = Range.inclusive(1, n, 1)

@Setup(Level.Trial)
def initTrial(): Unit = {
xs = fresh(size)
zs = Range.inclusive(-1, (-size / 1000) min -2, -1)
zipped = xs.map(x => (x, x))
if (size > 0) {
randomIndices = scala.Array.fill(1000)(scala.util.Random.nextInt(size))
}
Expand Down Expand Up @@ -176,4 +178,25 @@ class RangeBenchmark {
val result = xs.groupBy(_ % 5)
bh.consume(result)
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Int, b: Int) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = {
val xs1: IndexedSeq[Int] = xs
bh.consume(xs1.lazyZip(xs1).map((_, _)))
}

@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole

import scala.{Any, AnyRef, Int, Long, Unit, math}
import scala.Predef.intWrapper
import scala.{Any, AnyRef, Int, Long, Tuple2, Unit, math}
import scala.Predef.{intWrapper, $conforms, tuple2ToZippedOps}

@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
Expand Down Expand Up @@ -135,6 +135,21 @@ class ScalaHashSetBenchmark {
}
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Long, b: Long) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = bh.consume((xs, xs).zipped.map((_, _)))

@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip(t => (t._1, t._2)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole

import scala.{Any, AnyRef, Int, Long, Unit, math}
import scala.Predef.{intWrapper, $conforms}
import scala.{Any, AnyRef, Int, Long, Tuple2, Unit, math}
import scala.Predef.{intWrapper, $conforms, tuple2ToZippedOps}

@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
Expand Down Expand Up @@ -270,6 +270,21 @@ class ScalaListBenchmark {
}
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Long, b: Long) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = bh.consume((xs, xs).zipped.map((_, _)))

@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole

import scala.{Any, AnyRef, Int, Long, Unit, math}
import scala.Predef.intWrapper
import scala.{Any, AnyRef, Int, Long, Tuple2, Unit, math}
import scala.Predef.{intWrapper, tuple2ToZippedOps, $conforms}

@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
Expand Down Expand Up @@ -135,6 +135,21 @@ class ScalaTreeSetBenchmark {
}
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Long, b: Long) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = bh.consume((xs, xs).zipped.map((_, _)))

@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip(t => (t._1, t._2)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole

import scala.{Any, AnyRef, Int, Long, Unit, math}
import scala.Predef.{intWrapper, $conforms}
import scala.{Any, AnyRef, Int, Long, Tuple2, Unit, math}
import scala.Predef.{intWrapper, $conforms, tuple2ToZippedOps}

@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
Expand Down Expand Up @@ -270,6 +270,21 @@ class ScalaVectorBenchmark {
}
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Long, b: Long) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = bh.consume((xs, xs).zipped.map((_, _)))

@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ class TreeSetBenchmark {
}
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Long, b: Long) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = bh.consume(xs.lazyZip(xs).map((_, _)))

@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,21 @@ class VectorBenchmark {
}
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Long, b: Long) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = bh.consume(xs.lazyZip(xs).map((_, _)))

@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,21 @@ class ArrayBufferBenchmark {
}
}

@Benchmark
def transform_zip(bh: Blackhole): Unit = bh.consume(xs.zip(xs))

@Benchmark
def transform_zipMapTupled(bh: Blackhole): Unit = {
val f = (a: Long, b: Long) => (a, b)
bh.consume(xs.zip(xs).map(f.tupled))
}

@Benchmark
def transform_zipWithIndex(bh: Blackhole): Unit = bh.consume(xs.zipWithIndex)

@Benchmark
def transform_lazyZip(bh: Blackhole): Unit = bh.consume(xs.lazyZip(xs).map((_, _)))

@Benchmark
def transform_unzip(bh: Blackhole): Unit = bh.consume(zipped.unzip)

Expand Down
Loading