Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
12
Task/Time-a-function/Groovy/time-a-function-1.groovy
Normal file
12
Task/Time-a-function/Groovy/time-a-function-1.groovy
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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
|
||||
}
|
||||
5
Task/Time-a-function/Groovy/time-a-function-2.groovy
Normal file
5
Task/Time-a-function/Groovy/time-a-function-2.groovy
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def clockRealTime = { Closure c ->
|
||||
def start = System.currentTimeMillis()
|
||||
c.call()
|
||||
System.currentTimeMillis() - start
|
||||
}
|
||||
11
Task/Time-a-function/Groovy/time-a-function-3.groovy
Normal file
11
Task/Time-a-function/Groovy/time-a-function-3.groovy
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
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}"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue