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

Scala example source code file (TestUtil.scala)

This example Scala source code file (TestUtil.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

double, long, long, performance, t, t, testutil, testutil, unit, unit

The Scala TestUtil.scala source code

package scala.tools.partest

trait TestUtil {
  /** Given function and block of code, evaluates code block,
   *  calls function with nanoseconds elapsed, and returns block result.
   */
  def timed[T](f: Long => Unit)(body: => T): T = {
    val start = System.nanoTime
    val result = body
    val end = System.nanoTime
  
    f(end - start)
    result
  }
  /** Times body and returns (nanos, result).
   */
  def alsoNanos[T](body: => T): (Long, T) = {
    var nanos = 0L
    val result = timed(nanos = _)(body)
  
    (nanos, result)
  }
  def nanos(body: => Unit): Long = alsoNanos(body)._1
  
  def verifySpeed(body1: => Unit, body2: => Unit, acceptableMultiple: Double) = {
    val t1 = nanos(body1).toDouble
    val t2 = nanos(body2).toDouble
    val mult = if (t1 > t2) t1 / t2 else t2 / t1
    
    assert(mult <= acceptableMultiple, "Performance difference too great: multiple = " + mult)
  }
}

object TestUtil extends TestUtil {
  
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala TestUtil.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.