alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (eq.scala)

This example Scala source code file (eq.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

int, int, object, object, t, t

The Scala eq.scala source code

object eq extends testing.Benchmark {

  def eqtest[T](creator: Int => T, n: Int): Int = {
    val elems = Array.tabulate[AnyRef](n)(i => creator(i % 2).asInstanceOf[AnyRef])

    var sum = 0
    var i = 0
    while (i < n) {
      var j = 0
      while (j < n) {
        if (elems(i) eq elems(j)) sum += 1
        j += 1
      }
      i += 1
    }
    sum
  }

  val obj1 = new Object
  val obj2 = new Object

  def run() {
    var sum = 0
    sum += eqtest(x => if (x == 0) obj1 else obj2, 2000)
    sum += eqtest(x => x, 1000)
    sum += eqtest(x => x.toChar, 550)
    sum += eqtest(x => x.toByte, 550)
    sum += eqtest(x => x.toLong, 550)
    sum += eqtest(x => x.toShort, 100)
    sum += eqtest(x => x.toFloat, 100)
    sum += eqtest(x => x.toDouble, 100)
    assert(sum == 2958950)
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala eq.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.