From 10668e4cc7c59d98fca291b33007a9f83b775785 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Mon, 22 Mar 2021 15:16:28 +0100 Subject: [PATCH] Stopwatch --- .../de/bixilon/minosoft/util/Stopwatch.kt | 41 +++++++++++++++++++ .../de/bixilon/minosoft/util/UnitFormatter.kt | 17 ++++++-- 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/util/Stopwatch.kt diff --git a/src/main/java/de/bixilon/minosoft/util/Stopwatch.kt b/src/main/java/de/bixilon/minosoft/util/Stopwatch.kt new file mode 100644 index 000000000..1b7f63db8 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/util/Stopwatch.kt @@ -0,0 +1,41 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program.If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.util + +import de.bixilon.minosoft.util.logging.Log + +class Stopwatch { + val id = LAST_ID++ + private val startTime = System.nanoTime() + private val labs: MutableList = mutableListOf() + + /** + * @return Returns the difference between the last lab and their new one + */ + fun lab(): Long { + val currentTime = System.nanoTime() + val lastLab = labs.getOrNull(0) ?: startTime + labs.add(currentTime) + return currentTime - lastLab + } + + fun labPrint() { + val delta = lab() + Log.info("Stop watch ($id) lab: ${UnitFormatter.formatNanos(delta)}") + } + + companion object { + private var LAST_ID = 0 + } +} diff --git a/src/main/java/de/bixilon/minosoft/util/UnitFormatter.kt b/src/main/java/de/bixilon/minosoft/util/UnitFormatter.kt index fd6f16191..121ea4c63 100644 --- a/src/main/java/de/bixilon/minosoft/util/UnitFormatter.kt +++ b/src/main/java/de/bixilon/minosoft/util/UnitFormatter.kt @@ -14,8 +14,9 @@ package de.bixilon.minosoft.util object UnitFormatter { - private val BYTE_UNITS = listOf("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB") - private val UNITS = listOf("", "k", "M", "G", "T", "P", "E", "Z", "Y") + private val BYTE_UNITS = arrayOf("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB") + private val UNITS = arrayOf("", "k", "M", "G", "T", "P", "E", "Z", "Y") + private val TIME_UNITS = arrayOf("ns", "μs", "ms", "s", "m", "h", "d", "w", "M", "Y") fun formatBytes(bytes: Long): String { return formatUnit(bytes, BYTE_UNITS, 1024L) @@ -29,7 +30,7 @@ object UnitFormatter { return formatUnit(number, UNITS, 1000L) } - private fun formatUnit(number: Long, units: List, factor: Long): String { + private fun formatUnit(number: Long, units: Array, factor: Long): String { var lastFactor = 1L var currentFactor = factor for (unit in units) { @@ -44,4 +45,14 @@ object UnitFormatter { } throw IllegalArgumentException() } + + fun formatNanos(nanos: Long): String { + return "${nanos / 1000000}ms" + + // ToDo + } + + fun formatMillis(millis: Long): String { + return formatNanos(millis * 1000) + } }