Time a function: Difference between revisions

(→‎{{header|AutoHotkey}}: add QueryPerformanceCounter version)
(→‎{{header|Groovy}}: new solution)
Line 537:
643us
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
===CPU Timing===
<lang groovy>import java.lang.management.ManagementFactory
import java.lang.management.ThreadMXBean
 
def threadMX = ManagementFactory.threadMXBean
assert threadMX.currentThreadCpuTimeSupported
threadMX.threadCpuTimeEnabled = true
 
def clockCpuTime = { Closure c ->
def start = threadMX.currentThreadCpuTime
c.call()
(threadMX.currentThreadCpuTime - start)/1000000
}</lang>
 
===Wall Clock Timing===
<lang groovy>def clockRealTime = { Closure c ->
def start = System.currentTimeMillis()
c.call()
System.currentTimeMillis() - start
}</lang>
 
Test:
<lang groovy>def countTo = { Long n ->
long i = 0; while(i < n) { i += 1L }
}
 
["CPU time":clockCpuTime, "wall clock time":clockRealTime].each { measurementType, timer ->
println '\n'
[100000000L, 1000000000L].each { testSize ->
def measuredTime = timer(countTo.curry(testSize))
println "Counting to ${testSize} takes ${measuredTime}ms of ${measurementType}"
}
}</lang>
 
Output:
<pre>Counting to 100000000 takes 23150.5484ms of CPU time
Counting to 1000000000 takes 233861.0991ms of CPU time
 
 
Counting to 100000000 takes 24314ms of wall clock time
Counting to 1000000000 takes 249005ms of wall clock time</pre>
 
=={{header|Haskell}}==
Anonymous user