@@ -2,7 +2,7 @@ package kotlinx.benchmark.native
2
2
3
3
import kotlinx.benchmark.*
4
4
import kotlin.native.internal.GC
5
- import kotlin.system .*
5
+ import kotlin.time .*
6
6
7
7
class NativeExecutor (
8
8
name : String ,
@@ -218,25 +218,28 @@ class NativeExecutor(
218
218
return getStackTrace().joinToString(" \n " ) + " \n Cause: ${nested.message} \n " + nested.stacktrace()
219
219
}
220
220
221
+ @OptIn(ExperimentalTime ::class )
221
222
private inline fun measure (
222
223
cycles : Int ,
223
224
nativeGCAfterIteration : Boolean ,
224
225
body : () -> Unit ,
225
226
): Double {
226
- var counter = cycles
227
227
if (nativeGCAfterIteration)
228
228
GC .collect()
229
- val startTime = getTimeNanos()
230
- while (counter-- > 0 ) {
231
- body()
229
+
230
+ val duration = measureTime {
231
+ var counter = cycles
232
+ while (counter-- > 0 ) {
233
+ body()
234
+ }
235
+ if (nativeGCAfterIteration)
236
+ GC .collect()
232
237
}
233
- if (nativeGCAfterIteration)
234
- GC .collect()
235
- val endTime = getTimeNanos()
236
- val time = endTime - startTime
237
- return time.toDouble() / cycles
238
+
239
+ return duration.toDouble(DurationUnit .NANOSECONDS ) / cycles
238
240
}
239
241
242
+ @OptIn(ExperimentalTime ::class )
240
243
private inline fun <T > measureWarmup (
241
244
name : String ,
242
245
config : BenchmarkConfiguration ,
@@ -256,19 +259,18 @@ class NativeExecutor(
256
259
257
260
if (config.nativeGCAfterIteration)
258
261
GC .collect()
259
- val startTime = getTimeNanos ()
260
- var endTime = startTime
262
+ val startTime = TimeSource . Monotonic .markNow ()
263
+ var duration = Duration . ZERO
261
264
iterations = 0
262
- while (endTime - startTime < benchmarkNanos) {
265
+ while (duration.inWholeNanoseconds < benchmarkNanos) {
263
266
body()
264
- endTime = getTimeNanos ()
267
+ duration = startTime.elapsedNow ()
265
268
iterations++
266
269
}
267
270
if (config.nativeGCAfterIteration)
268
271
GC .collect()
269
272
270
- val time = endTime - startTime
271
- val metric = time.toDouble() / iterations // TODO: metric
273
+ val metric = duration.toDouble(DurationUnit .NANOSECONDS ) / iterations // TODO: metric
272
274
val sample = metric.nanosToText(config.mode, config.outputTimeUnit)
273
275
val iterationNumber = currentIteration ? : iteration
274
276
reporter.output(name, benchmark.name, " Warm-up #$iterationNumber : $sample " )
0 commit comments