From 015a94575f40896d51ec83a83d58362f9b47c76c Mon Sep 17 00:00:00 2001 From: Assasinnys Date: Mon, 11 May 2020 21:25:18 +0300 Subject: [PATCH] feat: add some stopwatch points --- .../kotlin/elite/alternative/AStarMainFile.kt | 17 +++++++++++++---- src/main/kotlin/elite/utils/Stopwatch.kt | 3 ++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/elite/alternative/AStarMainFile.kt b/src/main/kotlin/elite/alternative/AStarMainFile.kt index a85a715..6dcb263 100644 --- a/src/main/kotlin/elite/alternative/AStarMainFile.kt +++ b/src/main/kotlin/elite/alternative/AStarMainFile.kt @@ -41,13 +41,13 @@ class AStarMainFile { } do { - + val sw = Stopwatch().start() if (checkForFinish()) { println("${consoleStringCounter()} Finish found.") printTheFoundPath() return } - + sw.stopWithConsoleOutput("CheckForFinish time: ") val selectedStarPoint = findStarPointWithMinCost() findNeighbours(selectedStarPoint) openedList.remove(selectedStarPoint) @@ -69,15 +69,23 @@ class AStarMainFile { } private fun findStarPointWithMinCost(): StarPoint { + val sw = Stopwatch().start() return openedList.minBy { starPoint -> starPoint.costF }!!.also { nextStarPoint -> - println("Min cost star point: G = ${nextStarPoint.costG}, F = ${nextStarPoint.costF}, H = ${nextStarPoint.costH} " + - "dist = ${nextStarPoint.distance}, start = ${nextStarPoint.previousStarPoint == startStarPoint}") + sw.stopWithConsoleOutput("FIND MIN COST TIME: ") + println( + "Min cost star point: G = ${nextStarPoint.costG}, F = ${nextStarPoint.costF}, H = ${nextStarPoint.costH} " + + "dist = ${nextStarPoint.distance}, start = ${nextStarPoint.previousStarPoint == startStarPoint}" + ) } } private fun findNeighbours(starPoint: StarPoint) { + val sw1 = Stopwatch().start() + val sw2 = Stopwatch().start() val reader = file.bufferedReader().lines() + sw2.stopWithConsoleOutput("READ:LINES time: ") reader.forEach { line -> + val sArray = line.split(" ") val coords = Coordinates( sArray[1].toDouble(), @@ -122,6 +130,7 @@ class AStarMainFile { } } reader.close() + sw1.stopWithConsoleOutput("Find neighbors time (openListSize = ${openedList.size}): ") } private fun printTheFoundPath(): Int { diff --git a/src/main/kotlin/elite/utils/Stopwatch.kt b/src/main/kotlin/elite/utils/Stopwatch.kt index 532a1ba..1f0a1b1 100644 --- a/src/main/kotlin/elite/utils/Stopwatch.kt +++ b/src/main/kotlin/elite/utils/Stopwatch.kt @@ -3,8 +3,9 @@ package elite.utils class Stopwatch { private var startTime: Long = 0L - fun start() { + fun start(): Stopwatch { startTime = System.currentTimeMillis() + return this } fun stopWithConsoleOutput(prefix: String = "Time: ") {