Dennis Technologies

Members
  • Content count

    8
  • Joined

  • Last visited

Community Reputation

147 Neutral

About Dennis Technologies

  • Rank
    Newbie
  1. long vs int performance

    Thought I would share a recent micro-optimization I found for Android development (targeting Dalvik VM, not native). While trying to optimize some drawing code I noticed that Dalvik uses 32bit registers and thus register pairs (i.e. two registers per) for storage of 64bit data types like the scalar 'long'. Feeling that there might be a performance impact for having to access two registers, I decided to do some benchtesting with the following code: public void run() { int intCounter = 0; int i; long longCounter = 0; long ii; long elapseTime; long startTime; startTime = SystemClock.uptimeMillis(); for (int counter = 0; counter < 100; counter++) { for (i = 0; i < 100000; i++) { intCounter += i; } intCounter += counter; } elapseTime = SystemClock.uptimeMillis() - startTime; Log.d("IntLongTest", "Integer Ending Value - " + intCounter + " / Total Time - " + elapseTime); startTime = SystemClock.uptimeMillis(); for (long counter = 0; counter < 100; counter++) { for (ii = 0; ii < 100000; ii++) { longCounter += ii; } longCounter += counter; } elapseTime = SystemClock.uptimeMillis() - startTime; Log.d("IntLongTest", "Long Ending Value - " + longCounter + " / Total Time - " + elapseTime); } I came up with the following results: 09-14 23:11:18.567: D/IntLongTest(9491): Integer Ending Value - 1778798614 / Total Time - 123 09-14 23:11:18.728: D/IntLongTest(9491): Long Ending Value - 499995004950 / Total Time - 159 09-14 23:11:27.016: D/IntLongTest(9491): Integer Ending Value - 1778798614 / Total Time - 81 09-14 23:11:27.197: D/IntLongTest(9491): Long Ending Value - 499995004950 / Total Time - 175 09-14 23:11:34.044: D/IntLongTest(9491): Integer Ending Value - 1778798614 / Total Time - 33 09-14 23:11:34.214: D/IntLongTest(9491): Long Ending Value - 499995004950 / Total Time - 163 09-14 23:11:42.653: D/IntLongTest(9491): Integer Ending Value - 1778798614 / Total Time - 39 09-14 23:11:42.843: D/IntLongTest(9491): Long Ending Value - 499995004950 / Total Time - 193   The average execution time for integers is 68.75ms while the average execution time for longs is 172.5. Which means that longs are roughly 2.5 times slower than integers on my Galaxy S3 in this test. I do acknowledge it would be fairly unlikely that this would be a sole reason for poor performance in your code, but I thought it might be an interesting little tidbit :)   This did help me improve draw performance by roughly 10% because I was using longs to track animation frame dwell times for each individual sprite on the screen (I was benchtesting with 10,000 sprites).   Side note, you will notice the integer value is different from the long value which I believe is due to 'roll over' since the loop would take the integer variable past its maximum value.   If anybody else sees a different explanation I would love to hear it. This is just an idle observation, I have not ripped open the dex to see if the bytecode is acting different than expected or if the JIT is helping integers out somehow.
  2. Pathfinding on 7 oceans

    I was thinking about multiple heirarchy nav meshes after I posted. This is a good idea and would probably be how I solved the problem. The trick is to make each grid align with the next finer grid so that every node knows not only the 4 peer nodes attached to it but also the node it is sitting on top of in the next finer graph. Additionally you need to check when it is time to use the finer grained graph. I would do this as a fifth test where you are basically testing to see if it is better to stay at the current node instead of moving to the next node. An example of the implementation I would use: Assume a line of points numbered from 0 to 1000. I would create two graphs, one with every point evenly divisible by 100 (0, 100, 200,...) and one with every point that is evenly divisible by 10 (0, 10, ... 100, 110,...). You can see that the two graphs intersect at every one of the coarser grained graph's nodes. Now lets say I am standing at point 0 and want to move to point 240. I would start in the coarser grained graph and move to point 100 then to point 200. I can then see that I stay closer to my goal by staying at point 200 then by moving to 300 which would overshoot it. I then switch to the fine grained graph and I'm already standing at one of its nodes (200) so I then move to 210, 220, 230, and finally 240. Tada! I get cake :) I hope this made sense.
  3. The mystery of the spike

    Have you tried reading all of the data in one go or maybe bigger chunks? I'm wondering if you are getting disk seeks from other processes while you are decompressing. Causing your process to constantly have to seek back to the point in the file it left off when it goes back for more. I've found that file I/O is a get in, do the job, and get out process with no lollygagging inbetween. I could be way off base though and I wouldn't consider myself an expert on file systems. Also, is your hard drive severely fragmented? This will cause poor performance with file I/O as well.
  4. Pathfinding on 7 oceans

    I suggest perusing the AI forum. There was a recent post there with a link to a fantastic Gamasutra article all about pathfinding. Off the top of my head I would recommend this: 1. Break the world map into a grid, like a globe, with nodes at every intersection of latitudinal and longitudinal lines. 2. Every node should know how to get to the 4 nodes attached to it. 3. Calculate the closest node to your destination. 4. Calculate the closest node to your origin. 5. Calculate a path from the origin node to the destination node. 6. Use A* to get your ship to the origin node. 7. Use A* to get your ship from the destination node to the actual destination. The trick here is that steps 1 & 2 can be be precomputed at compile time. I believe this is called a navmesh. Also I am assuming there is just open water between any two points and no land masses. Introducing intervening land masses will complicate step 5 slightly but not by much. It will be up to you find the optimum size of the grid.
  5. Component based architecture and messaging

    Most people will tend towards decoupling out of efficiency, being easier to change components. Coupling can work, and can (I stress 'can') be faster to implement as well as be faster overall, however your system will be less robust and will be far more fragile. Your second idea does not require 'coupling' as I would understand it. Have you looked at Service Oriented Architectures? I think it would fit well and is a decoupled system.
  6. Resizing game objects and components arrays

    Use a two dimensional array to store your objects. Essentially it looks like this: Object_array [CURRENT_LEVEL] = all objects in current area Object_array[NEXT_LEVEL] = all objects in next area You can then do a swap to change CURRENT and NEXT when needed.
  7. Subtract timestamp time from current time

    The php manual is awesome for this. date_diff returns either a DateInterval object or a boolean. To calculate just hours I would use this formula: hours = (days * 24) + (h) + (i / 60) + (s / 3600) Some notes: 1. Test for failure before using the DateInterval or you might throw exceptions. 2. DateInterval-> days is an integer rounded down. 3. Don't do the division in a loop. Precalc to a constant and multiply. EDIT: Also if you are looking for straight linear time diffs then I would avoid timestamps in date formats all together since you can run into funky 'leap' issues. Store your timestamps in milliseconds since epoch and divide from there.