My relatively simple game is clogging up, once I add more entities. What do?

I have this game: http://www.java-gaming.org/topics/iconified/26136/view.html
…and it seems to clog up the FPS, once I make the map bigger, but worst is it when I begin to use pathfinding with just six entities.

Problem is, I dont know exactly where it happens. It could be my heuristic, it could be too many pathfinding checks - I cant tell.
Is there a way to check what code executes slower/where in the code there might be a bottleneck?

Also, I know my getNearestNeighbor function sucks, and takes like 4 times more heuristic calls than it needs to.


/**
	 * Returns the path the nearest neighbor.
	 * @param sx Source x coord
	 * @param sy Source y coord
	 * @param tx Target x coord
	 * @param ty Target y coord
	 * @param map Map we're moving around in
	 * @return The coords of the nearest neighbor
	 */
	public Path nearestNeighbor(Mover mover, int sx, int sy, int tx, int ty) {	
		
		Path closest = null;
		int stepAmount = Integer.MAX_VALUE;
		
		for (int x=-1;x<2;x++) {
			for (int y=-1;y<2;y++) {
				if ((x == 0) && (y == 0)) { // it's not a neighbour
					continue;
				}

				if ((x != 0) && (y != 0)) { // no diagonal movement
						continue;
				}	
				
				// determine the location of the neighbour and evaluate it
				int xp = x + tx;
				int yp = y + ty;
				if (isValidLocation(mover, sx, sy, xp, yp)) {
					Path path = findPath(mover, sx, sy, xp, yp);
					if (path != null) { // If there was not a path, continue on, adventurer!
						int steps = path.getLength();
						if (steps < stepAmount) { // If it has less steps there than anything before
							closest = findPath(mover, sx, sy, xp, yp);
							stepAmount = steps;
						}
					}
				}	
			}
		} // We've run out of search folks!
		if (closest != null) { // Something was found!
			return closest;
		} else { // Nothing was found!
			return null;
		}
	}

I’m rendering a lot of images too, from a lot of different sheets. Should I pack them all on one sheet, so I can call drawEmbedded in Slick?
Does that matter a lot at all? When I look at my map, I only get 45 fps/60 fps.

Should I use multithreading for this heavy logic? Help! :smiley:

I can upload source if you want to check yourself :slight_smile:

This was my old heuristic:


float dx = tx - x;
float dy = ty - y;
		
float result = (float) (Math.sqrt((dx*dx)+(dy*dy)));

…and I cant notice the difference with this one:


float dx = Math.abs(tx - x);
float dy = Math.abs(ty - y);
float result = dx+dy;

Hi a general advice: make a performance Checker / Display for each code part that draws Performance.
(Performance Metric)

Thus that you check how much time each part (rendering, gamelogic, specific functions like the pathfinding) uses up,
and display these (averaged) values ingame.

it can be as simple as

timeMSPathfinding=System.currentTimeMillis(); //or use System.nanoTime()…

do your pathfindin stuff

timeMSPathfinding=System.currentTimeMillis()-timeMSPathfinding;
-> now containing the Milliseconds used to run the method

Then average these values over some cycles, and display them.
-> This is a good Method to optimize where its needed, and also see (not just guess) a feedback to your changes.

I often fear that some codechange takes much more time than before, then realising by the numbers it almost di not change.
Or opposite: you can make out performance-hungry functions that are not easily noticable before. Taking small but many chunks of your performance.

Once in place -> really cut down on you OOP structures. you use getters, setters and Sorted Lists and all that mumbo in
a massive pathfinding loop.
Now any heavy loop like pathfinding is where “clean” programming slows you down noticably.
You can code clean outside of massive itterations, inside of them try to really optimize the code, and avoid unnessasary layers.

Large itterations, things regularily running in the gameloop, and up-scalable parts (like entities to be added in large numbers) : optimize

Functions that are used only sometimes, initialization routines, loading etc : dont optimize

I don’t know a lot about performance checking except to use JVisualVM.exe (in the jdk bin file). One can also put in some timestamps (use nano level, not millis which are usually much less accurate due to OS implementations) and print them to the console or file for inspection.

Part of what prompted me to write was the heuristic. The following might be more accurate, if the point is to rank 2D distances via the return value:


float dx = tx - x;
float dy = ty - y;
float result = (dx * dx) + (dy * dy);

If you just add dx and dy, even as absolute values, it won’t be as accurate for ranking distances as it would if you multiply each value first. Maybe you’ll save, dropping the Math.abs calls, what you lose by multiplying. But it doesn’t seem like that should effect performance very much.