feat: add stopwatch class, some small fixes

This commit is contained in:
Assasinnys 2020-05-11 14:53:07 +03:00
parent a7af2546dc
commit cdfe160aee
5 changed files with 42 additions and 20 deletions

View File

@ -1 +1 @@
# ED-AStar-Galaxy-Router
# ED-AStar-Galaxy-Router codename "IGOR"

View File

@ -20,7 +20,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import sun.audio.AudioPlayer
import java.io.File
import java.io.FileInputStream
import java.nio.file.Files
@ -67,7 +66,7 @@ var replaces = 0
fun calcTime(timeStart: Long): Double = System.currentTimeMillis().minus(timeStart).div(1000.0)
fun startCoroutineQuery(systemName: String) {
/*fun startCoroutineQuery(systemName: String) {
val coroutineScope = CoroutineScope(Dispatchers.Default)
coroutineScope.launch {
@ -88,7 +87,7 @@ fun startCoroutineQuery(systemName: String) {
println("Coroutine complete query ${systemName}. Time: ${calcTime(startCoroutineTime)}")
database.closeDB()
}
}
}*/
/*fun addToResultList(result: Int) {
@ -183,17 +182,6 @@ fun startCoroutineQuery(systemName: String) {
//TODO alternative MAIN with work in file
/*fun main(args: Array<String>) {
println("Start A Star with file table")
val thread = Thread{
val inputStream = FileInputStream("sound.mp3")
val player = Player(inputStream)
try {
while (true) {
player.play()
}
} finally {
player.close()
}
}.apply { isDaemon = true }.start()
val time = System.currentTimeMillis()
println("Distance modifier $DISTANCE_MODIFIER")

View File

@ -25,6 +25,7 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
private val openedList = mutableListOf<StarPoint>()
private val closedList = mutableListOf<StarPoint>()
private val stopwatch = Stopwatch()
init {
openedList.add(startStarPoint)
@ -74,15 +75,18 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
}
private fun findStarPointWithMinCost(): StarPoint {
stopwatch.start()
return openedList.minBy { starPoint -> starPoint.costF }!!.also { nextStarPoint ->
println("Min cost star point: G = ${nextStarPoint.costG}, F = ${nextStarPoint.costF}, " +
"dist = ${nextStarPoint.distance}, start = ${nextStarPoint.previousStarPoint == startStarPoint}")
stopwatch.stopWithConsoleOutput("Min cost find time: ")
}
}
private fun findNeighbours(starPoint: StarPoint) {
checkConnection()
stopwatch.start()
val resultSet = database.query(
"select $C_ID64, $C_X, $C_Y, $C_Z, $C_SUBTYPE = 'Neutron Star' as isNeutronStar, " +
"$C_SYS_NAME, " +
@ -93,6 +97,10 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
)}" +
"and not $C_ID64=${starPoint.systemId64}"
)
stopwatch.stopWithConsoleOutput("Query time: ")
stopwatch.start()
val sw2 = Stopwatch()
while (resultSet.next()) {
with(resultSet) {
@ -103,14 +111,17 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
), getBoolean("isNeutronStar"), getDouble("dist"),
getString(C_SYS_NAME), starPoint.jumpCounter.plus(1), finishStarPoint.coords
)
sw2.start()
if (closedList.notContains(newStarPoint)) {
// openedList.addIfAbsent(newStarPoint)
// openedList.smartAdd(newStarPoint)
openedList.smartAdd2(newStarPoint)
}
sw2.stopWithConsoleOutput("Add to openList time: ")
}
}
resultSet.close()
stopwatch.stopWithConsoleOutput("Process time: ")
}
private fun isNeutronDistance(isNeutron: Boolean) = if (isNeutron) NEUTRON_DISTANCE else USUAL_DISTANCE

View File

@ -32,14 +32,14 @@ class AStarMainFile {
findNeighbours(startStarPoint)
openedList.remove(startStarPoint)
closedList.add(startStarPoint)
if (openedList.isEmpty()) {
println("${consoleStringCounter()}Unable to complete task. No neighbors found near startStarPoint. =(")
return
}
openedList.remove(startStarPoint)
closedList.add(startStarPoint)
do {
if (checkForFinish()) {
@ -70,7 +70,7 @@ class AStarMainFile {
private fun findStarPointWithMinCost(): StarPoint {
return openedList.minBy { starPoint -> starPoint.costF }!!.also { nextStarPoint ->
println("Min cost star point: G = ${nextStarPoint.costG}, F = ${nextStarPoint.costF}, " +
println("Min cost star point: G = ${nextStarPoint.costG}, F = ${nextStarPoint.costF}, H = ${nextStarPoint.costH} " +
"dist = ${nextStarPoint.distance}, start = ${nextStarPoint.previousStarPoint == startStarPoint}")
}
}
@ -125,19 +125,25 @@ class AStarMainFile {
}
private fun printTheFoundPath(): Int {
// val db = Database().apply { openConnection() }
var starPoint = finishStarPoint.previousStarPoint!!
var counter = 0
var fullDistance = 0.0
while (starPoint != startStarPoint) {
// val systemName = db.query("select ${Database.C_SYS_NAME} FROM ${AStarMain.CORRIDOR} WHERE ${Database.C_ID64} = ${starPoint.systemId64}").let {
// it.next()
// it.getString(Database.C_SYS_NAME)
// }
println(
"${consoleStringCounter()} Point $counter id = ${starPoint.systemId64}, " +
// "name = ${starPoint.systemName} " +
// "name = $systemName " +
"distance = ${starPoint.distance}, G = ${starPoint.costG}, F = ${starPoint.costF}"
)
fullDistance += starPoint.distance
starPoint = starPoint.previousStarPoint!!
counter++
}
// db.closeDB()
println("${consoleStringCounter()} Total jumps counter = $counter, distance = $fullDistance ly, replaces = $replaces, cof = ${StarPoint.NEUTRON_COF}")
return counter
}

View File

@ -0,0 +1,17 @@
package elite.utils
class Stopwatch {
private var startTime: Long = 0L
fun start() {
startTime = System.currentTimeMillis()
}
fun stopWithConsoleOutput(prefix: String = "Time: ") {
if (startTime == 0L) throw Exception("Not invoke fun start()")
val time: Double = System.currentTimeMillis().minus(startTime).toDouble().div(1000)
println("$prefix $time")
startTime = 0L
}
}