stdlib: Add util methods for time measurements
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
#include <chrono>
|
||||
#include "Types.h"
|
||||
#include "Natives.h"
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
extern "C" {
|
||||
|
||||
KLong Kotlin_system_getTimeMillis() {
|
||||
return duration_cast<milliseconds>(high_resolution_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
KLong Kotlin_system_getTimeNanos() {
|
||||
return duration_cast<nanoseconds>(high_resolution_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
KLong Kotlin_system_getTimeMicros() {
|
||||
return duration_cast<microseconds>(high_resolution_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,34 @@
|
||||
package kotlin.system
|
||||
|
||||
@PublishedApi
|
||||
@SymbolName("Kotlin_system_getTimeMillis")
|
||||
internal external fun getTimeMillis() : Long
|
||||
|
||||
@PublishedApi
|
||||
@SymbolName("Kotlin_system_getTimeNanos")
|
||||
internal external fun getTimeNanos() : Long
|
||||
|
||||
@PublishedApi
|
||||
@SymbolName("Kotlin_system_getTimeMicros")
|
||||
internal external fun getTimeMicros() : Long
|
||||
|
||||
/** Executes the given block and returns elapsed time in milliseconds. */
|
||||
public inline fun measureTimeMillis(block: () -> Unit) : Long {
|
||||
val start = getTimeMillis()
|
||||
block()
|
||||
return getTimeMillis() - start
|
||||
}
|
||||
|
||||
/** Executes the given block and returns elapsed time in microseconds (Kotlin/Native only). */
|
||||
public inline fun measureTimeMicros(block: () -> Unit) : Long {
|
||||
val start = getTimeMicros()
|
||||
block()
|
||||
return getTimeMicros() - start
|
||||
}
|
||||
|
||||
/** Executes the given block and returns elapsed time in nanoseconds. */
|
||||
public inline fun measureNanoTime(block: () -> Unit) : Long {
|
||||
val start = getTimeNanos()
|
||||
block()
|
||||
return getTimeNanos() - start
|
||||
}
|
||||
Reference in New Issue
Block a user