Skip to content

Instantly share code, notes, and snippets.

@stepancheg
Created November 23, 2009 17:15
Show Gist options
  • Save stepancheg/241204 to your computer and use it in GitHub Desktop.
Save stepancheg/241204 to your computer and use it in GitHub Desktop.
Collections.sort
case class A(value: Int)
def sort(a: java.util.List[A]) = {
val c = new java.util.Comparator[A] {
override def compare(x: A, y: A) = x.value - y.value
}
java.util.Collections.sort(a, c)
}
def profile[T](what: String)(action: => T) = {
val start = System.currentTimeMillis()
val r = action
val delta = System.currentTimeMillis() - start
println(what + ": " + delta + "; " + r)
}
while (true) {
val a = new java.util.ArrayList[A]
val r = new java.util.Random
for (i <- 1 to 20 * 1000 * 1000) {
a.add(new A(r.nextInt()))
}
profile("sort")(sort(a))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment