Removed sets for a mild speed boost and changed a repeatedly-created val to a var, shaving off a couple dozen ms on average

main
Bryson Zimmerman 11 months ago
parent 2a433508c7
commit 6b31dfabb8

@ -25,18 +25,18 @@ class HierarchicalPathfinding {
val ArrayBackedPathfinderTime = measureTime {
ArrayBackedPathfinder.generatePath(Tile(0, 0), Tile(n - 1, (n - 1)))
}
/*
val MapBackedPathfinderTime = measureTime {
MapBackedPathfinder.generatePath(Tile(0, 0), Tile(n - 1, (n - 1)))
}
*/
println(World.toString())
println(n*n)
println("Maze build time: ${buildMazeTime.inWholeMilliseconds} ms")
//println("HashMap-Backed Pathfinder time: ${MapBackedPathfinderTime.inWholeMilliseconds}ms")
println("Array-Backed Pathfinder time: ${ArrayBackedPathfinderTime.inWholeMilliseconds}ms")
println("BFS Pathfinder time: ${BFSPathFinderTime.inWholeMilliseconds}ms")
println("Array-Backed Pathfinder time: ${ArrayBackedPathfinderTime.inWholeMilliseconds}ms")
println("HashMap-Backed Pathfinder time: ${MapBackedPathfinderTime.inWholeMilliseconds}ms")
}
//Clear the maze of pathfinding markers before running another pathfinding algorithm

@ -39,7 +39,7 @@ object MazeFinder {
var current: Tile
var inGraph: Tile
var adjacentExplored: Set<Tile>
var adjacentExplored: ArrayList<Tile>
while(frontier.isNotEmpty()) {
//Grab a random tile from the frontier
val random = randGen.nextInt(frontier.size)

@ -43,8 +43,8 @@ value class Tile(private val value: ULong) {
}
//Get adjacent tiles for Prim's Algorithm
fun getAdjacentTiles(explored:Boolean): Set<Tile> {
val adj = mutableSetOf<Tile>()
fun getAdjacentTiles(explored:Boolean): ArrayList<Tile> {
val adj = arrayListOf<Tile>()
val dirs = Directions.ALL
dirs.forEach { dir ->
@ -115,8 +115,8 @@ value class Tile(private val value: ULong) {
}
//Get connections for pathfinding algorithms
fun getConnections(): Set<Tile> {
val connections = mutableSetOf<Tile>()
fun getConnections(): ArrayList<Tile> {
val connections = arrayListOf<Tile>()
val properties = getProperties()
if(properties.connections and(Directions.UP.dir) != 0) {
connections.add(this + Directions.NORTH)

@ -68,8 +68,10 @@ class TileHeap(val end: Tile, val fValue:(Tile, Tile) -> Int) {
private fun leftChild(index: Int): Int = index * 2
private fun rightChild(index: Int): Int = (index * 2) + 1
//Instead of allocating the memory every time, do it once.
var index1tmp:Tile = Tile(0, 0)
private fun swap(index1: Int, index2: Int) {
val index1tmp = dat[index1]
index1tmp = dat[index1]
dat[index1] = dat[index2]
dat[index2] = index1tmp
}

Loading…
Cancel
Save