LoginSignup
0
0

More than 5 years have passed since last update.

scala - 重複除去

Last updated at Posted at 2015-01-16

distinctは遅いみたいです。 嘘でしたすいません.. (xuwei_kさんありがとうございます)

class Bench {
  @Benchmark
  def set2seq(): Unit = (1 to 100).combinations(2).toList.flatten.toSet.toSeq

  @Benchmark
  def distinct(): Unit = (1 to 100).combinations(2).toList.flatten.distinct
}
[info] Benchmark           Mode  Samples    Score  Score error  Units
[info] f.Bench.set2seq    thrpt        3   90.893      495.577  ops/s
[info] f.Bench.distinct   thrpt        3  109.927       75.496  ops/s

正しくないやつ↓

@State(Scope.Thread)
class Bench {
  val list: Seq[Int] = (1 to 100).combinations(2).toList.flatten

  @Benchmark
  def set2seq(): Unit = list.toSet.toSeq

  @Benchmark
  def distinct(): Unit = list.distinct
}
[info] Benchmark            Mode  Samples      Score  Score error  Units
[info] f.Bench.distinct    thrpt        3  13111.451     3086.943  ops/s
[info] f.Bench.set2seq     thrpt        3   5717.012     4046.475  ops/s

benchmarkの問題っぽい。リストを作るタイミングの問題かな...

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0