From 0e197e89ac96ec0dd8b136de8e07ad2e15f94371 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 9 May 2012 15:01:26 -0700 Subject: Custom hashCode methods for case classes. No boxing, no MODULE$ indirection. --- test/files/run/caseClassHash.scala | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/files/run/caseClassHash.scala (limited to 'test/files/run/caseClassHash.scala') 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.") + } +} -- cgit v1.2.3