Created
April 17, 2017 18:03
-
-
Save BrianLondon/2bad831eeeb1b7a7d65584a276231f20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots" | |
// libraryDependencies += "com.github.ichoran" % "thyme_2.12" % "0.1.2-SNAPSHOT" | |
import ichi.bench._ | |
import java.util.UUID | |
object OrderingByDemo extends App { | |
println(util.Properties.versionString) | |
// Create a custom data type Foo and fill an Array with arbitrary values | |
case class Foo(int: Int, number: Long, string: String) | |
val r = scala.util.Random | |
r.setSeed(0L) | |
val array: Array[Foo] = Array.fill(1000000) { | |
val uuid = java.util.UUID.randomUUID() | |
Foo(r.nextInt, uuid.getLeastSignificantBits, uuid.toString) | |
} | |
// Define equivalent Orderings different ways on Foo | |
val orderingBy: Ordering[Foo] = Ordering.by(_.int) | |
val orderingFromLessThan: Ordering[Foo] = Ordering.fromLessThan(_.int < _.int) | |
val orderingCustom: Ordering[Foo] = new Ordering[Foo] { | |
override def compare(x: Foo, y: Foo): Int = { | |
if (x.int < y.int) -1 | |
else if (y.int < x.int) 1 | |
else 0 | |
} | |
} | |
// Run microbenchmarks | |
val th = Thyme.warmed(verbose = print) | |
th.pbench(array.sorted(orderingBy), title="Ordering.by") | |
th.pbench(array.sorted(orderingFromLessThan), title="Ordering.fromLessThan") | |
th.pbench(array.sorted(orderingCustom), title="custom Ordering") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment