Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  My relatively simple game is clogging up, once I add more entities. What do?  (Read 783 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Posted 2012-04-04 05:15:37 »

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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
/**
    * 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! Cheesy  

I can upload source if you want to check yourself Smiley

This was my old heuristic:
1  
2  
3  
4  
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:
1  
2  
3  
float dx = Math.abs(tx - x);
float dy = Math.abs(ty - y);
float result = dx+dy;

Offline Damocles
« Reply #1 - Posted 2012-04-04 05:50:06 »

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

Offline philfrei
« Reply #2 - Posted 2012-04-04 07:11:04 »

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:

1  
2  
3  
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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (13 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (32 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (19 views)
2014-08-16 06:20:21

Tekkerue (29 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (42 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!