Microefficiency

Discussion in 'Performance Tweaking' started by Rixterz, May 7, 2017.

  1. Hi,

    I have a HashMap<UUID, EndWarsPlayer> called endWarsPlayers.
    EndWarsPlayer contains a Player field, accessed by getPlayer().

    Which would be the most efficient way to get the Player by their UUID?

    1) endWarsPlayers.get(uuid).getPlayer()
    2) Bukkit.getServer().getPlayer(uuid);

    - Rix
     
  2. electronicboy

    IRC Staff

    Both have a map lookup, both have method calls, you're pretty much entering the level of difference between the two where the only difference is potentially the number of operations that occur in bytecode, as well as the context of your current situation. (e.g. do you already have an EndWarsPlayer class in scope reference for the CraftPlayer you want).
     
  3. no clue why your having any problems sorry
     
    • Funny Funny x 1
  4. ?
     
  5. A) They're not having any problems and B) If you have no clue, even if they did have a problem, don't bother responding to say that. Common sense xD
     
    • Funny Funny x 1
  6. Optimise your code elsewhere. It's pretty unlikely that you'll have to invoke this method very often. There are better places to optimise - Like code blocks inside very nested loops or chunk loading
     
  7. Test it yourself.

    Code (Text):
    long start = System.currentTimeMillis();

    // getPlayer..

    System.out.println("Time taken: " + (System.currentTimeMillis() - start) + "ms.");
     
  8. MiniDigger

    Supporter

    you will not be able to measure anything in the ms range, lol. its a simple hashmap lookup, those take nanoseconds.
    also, thats really not how you benchmark java....
     
  9. Code (Text):
    System.nanoTime()
    ?
     
  10. It's still silly to try and benchmark like that (Unless you're benchmarking something that takes time that is in a magnitude of seconds.. then the overhead and difference is negligible). This guy details a few of the problems he ran into here:

    https://baptiste-wicht.com/posts/2010/04/write-corrects-benchmarks.html

    Even then, I wouldn't trust his benchmarking technique. The JVM is a complex beast.
     
  11. MiniDigger

    Supporter

    thats why java now got a proper benchmarking framework Java Microbenchmarking Harness (JMH)