summaryrefslogtreecommitdiff
path: root/test/files/run/caseClassHash.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2012-05-09 15:01:26 -0700
committerPaul Phillips <paulp@improving.org>2012-05-09 16:32:51 -0700
commit0e197e89ac96ec0dd8b136de8e07ad2e15f94371 (patch)
tree77bb056d6c7d2c48f8b5ff9102c60a9db6862277 /test/files/run/caseClassHash.scala
parent09f380dbda56abdfbdda0cab51bc7187eb70b516 (diff)
downloadscala-0e197e89ac96ec0dd8b136de8e07ad2e15f94371.tar.gz
scala-0e197e89ac96ec0dd8b136de8e07ad2e15f94371.tar.bz2
scala-0e197e89ac96ec0dd8b136de8e07ad2e15f94371.zip
Custom hashCode methods for case classes.
No boxing, no MODULE$ indirection.
Diffstat (limited to 'test/files/run/caseClassHash.scala')
-rw-r--r--test/files/run/caseClassHash.scala37
1 files changed, 37 insertions, 0 deletions
diff --git a/test/files/run/caseClassHash.scala b/test/files/run/caseClassHash.scala
new file mode 100644
index 0000000000..7adfddedf8
--- /dev/null
+++ b/test/files/run/caseClassHash.scala
@@ -0,0 +1,37 @@
+case class Foo[T](a: Boolean, b: Byte, c: Short, d: Char, e: Int, f: Long, g: Double, h: Float, i: AnyRef, j: T) { }
+
+object Test {
+ def mkFoo[T](x: T) = Foo[T](true, -1, -1, 100, -5, -10, 500d, 500f, Nil, x)
+
+ def main(args: Array[String]): Unit = {
+ val foo1 = mkFoo[Double](5.0d)
+ val foo2 = mkFoo[Long](5l)
+
+ List(foo1, foo2, foo1.##, foo2.##, foo1 == foo2) foreach println
+
+ println("## method 1: " + foo1.##)
+ println("## method 2: " + foo2.##)
+ println(" Murmur 1: " + scala.util.MurmurHash3.productHash(foo1))
+ println(" Murmur 2: " + scala.util.MurmurHash3.productHash(foo2))
+ }
+}
+
+object Timing {
+ var hash = 0
+ def mkFoo(i: Int) = Foo(i % 2 == 0, i.toByte, i.toShort, i.toChar, i, i, 1.1, 1.1f, this, this)
+
+ def main(args: Array[String]): Unit = {
+ val reps = if (args.isEmpty) 100000000 else args(0).toInt
+ val start = System.nanoTime
+
+ println("Warmup.")
+ 1 to 10000 foreach mkFoo
+
+ hash = 0
+ 1 to reps foreach (i => hash += mkFoo(i).##)
+
+ val end = System.nanoTime
+ println("hash = " + hash)
+ println("Elapsed: " + ((end - start) / 1e6) + " ms.")
+ }
+}