object PerformanceEvaluation
Collection of helper functions useful when evaluating the performance of some code.
- Source
- PerformanceEvaluation.scala
Measuring the time and memory used by some piece of code:
import org.opalj.util.PerformanceEvaluation.{memory,time} var store : Array[Object] = null implicit val logContext: Option[LogContext] = Some(org.opalj.log.GlobalLogContext) for(i <- 1 to 5){ memory{store = null}(l => println("empty: "+l)) memory{ time{ store = Array.fill(1000000){val l : Object = List(i); l} }(t => println("time:"+t.toSeconds)) }(l => println("non-empty:"+l)) }
- Alphabetic
- By Inheritance
- PerformanceEvaluation
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @IntrinsicCandidate()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @IntrinsicCandidate()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @IntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def memory[T](f: => T)(mu: (Long) => Unit)(implicit logContext: Option[LogContext] = None): T
Measures the amount of memory that is used as a side-effect of executing the given function
f.Measures the amount of memory that is used as a side-effect of executing the given function
f. I.e., the amount of memory is measured that is used before and after executingf; i.e., the permanent data structures that are created byfare measured.- Note
If large data structures are used by
fthat are not used anymore afterwards then it may happen that the used amount of memory is negative.
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @IntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @IntrinsicCandidate()
- def run[T, X](f: => T)(r: (Nanoseconds, T) => X): X
Times the execution of a given function
f.Times the execution of a given function
f.- r
A function that is passed the time that it took to evaluate
fand the result produced byf;ris only called iffsucceeds.
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def time[T](epsilon: Int, consideredRunsEpsilon: Int, minimalNumberOfRelevantRuns: Int, f: => T, runGC: Boolean = false)(r: (Nanoseconds, Seq[Nanoseconds]) => Unit): T
Times the execution of a given function
funtil the execution time has stabilized and the average time for evaluatingfis only changing in a well-understood manner.Times the execution of a given function
funtil the execution time has stabilized and the average time for evaluatingfis only changing in a well-understood manner.In general,
timerepeats the execution offas long as the average changes significantly. Furthermore,fis executed at leastminimalNumberOfRelevantRunstimes and only those runs are taken into consideration for the calculation of the average that areconsideredRunsEpsilon% worse than the best run. However, if we have more than10*minimalNumberOfRelevantRunsruns that did not contribute to the calculation of the average, the last run is added anyway. This way, we ensure that the evaluation will more likely terminate in reasonable time without affecting the average too much. Nevertheless, if the behavior offis extremely erratic, the evaluation may not terminate.Example Usage
import org.opalj.util.PerformanceEvaluation._ import org.opalj.util._ time[String](2,4,3,{Thread.sleep(300).toString}){ (t, ts) => val sTs = ts.map(t => f"${t.toSeconds.timeSpan}%1.4f").mkString(", ") println(f"Avg: ${avg(ts).timeSpan}%1.4f; T: ${t.toSeconds.timeSpan}%1.4f; Ts: $sTs") }
import org.opalj.util.PerformanceEvaluation.{gc,memory,time,avg} var store : Array[Object] = null implicit val logContext: Option[LogContext] = Some(org.opalj.log.GlobalLogContext) time{ for(i <- 1 to 5){ memory{store = null}(l => println("empty: "+l)) memory{ time(2,4,3, {store = Array.fill(1000000){val l : Object = List(i); l}}, runGC=true ){ (t, ts) => val sTs = ts.map(t => f"${t.toSeconds.timeSpan}%1.4f").mkString(", ") println(f"Avg: ${avg(ts).timeSpan}%1.4f; T:${t.toSeconds.timeSpan}%1.4f; Ts:$sTs") } }(l => println("non-empty:"+l)) } }{t => println("overall-time:"+t.toSeconds)}
- epsilon
The maximum percentage that *the final run* is allowed to affect the average. In other words, if the effect of the last execution on the average is less than
epsilonpercent then the evaluation halts and the result of the last run is returned.- consideredRunsEpsilon
Controls which runs are taken into consideration when calculating the average. Only those runs are used that are at most
consideredRunsEpsilon%slower than the last run. Additionally, the last run is only considered if it is at mostconsideredRunsEpsilon%slower than the average. Hence, it is even possible that the average may rise during the evaluation off.- f
The side-effect free function that will be measured.
- runGC
If
truethe garbage collector is run usingorg.opalj.util.gc()before each run. This may be necessary to get reasonable stable behavior between multiple runs. However, if each run takes very long and the VM has to perform garbage collection as part of executing f (and also has to increase the JVM's heap) getting stable measurements is unlikely.- r
A function that is called back whenever
fwas successfully evaluated. The signature is:def r( lastExecutionTime:Nanoseconds, consideredExecutionTimes : Seq[Nanoseconds] ) : Unit
- The first parameter is the last execution time of
f. - The last parameter is the list of times required to evaluate
fthat are taken into consideration when calculating the average.
- The first parameter is the last execution time of
- Note
**If
,fhas side effects it may not be possible to use this method.**If epsilon is too small we can get an endless loop as the termination condition is never met. However, in practice often a value such as "1 or 2" is still useable.
,This method can generally only be used to measure the time of some process that does not require user interaction or disk/network access. In the latter case the variation between two runs will be too coarse grained to get meaningful results.
- def time[T](f: => T)(r: (Nanoseconds) => Unit): T
Times the execution of a given function
f.Times the execution of a given function
f. If the timing may be affected by (required) garbage collection runs it is recommended to first run the garbage collector.- r
A function that is passed the time (in nanoseconds) that it took to evaluate
f.ris called even ifffails with an exception.
- def timed[T](f: => T): (Nanoseconds, T)
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated @Deprecated
- Deprecated