Solved Just a performance question

Discussion in 'Plugin Development' started by padili, Jun 14, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    padili

    Hey Guys, I'm currently developing a plugin to set custom damage based on the custom item.
    What is faster? To read out the the values by stripping the color and replacing other values like this:
    Code:java
    1. ChatColor.stripColor(lore.get(0)).replace("dmg", "")

    or to get the value from the config like that:
    Code:java
    1. ConfigurationSection cf = config.getConfigurationSection("items."+name);
    2. int damageMIN = cf.getInt("damage.min");
    3. int damageMAX = cf.getInt("damage.max");

    I have to check it everytime a player hits an entity.
     
  2. It's a matter of ms, I assume they both take the same average amount of time.
     
  3. Offline

    xTrollxDudex

    padili
    Top. Keep in mind that cached values and memory operations will never be slower than IO, unless of course, you're doing a heavy call.
     
  4. Offline

    xTigerRebornx

    xTrollxDudex As long he doesn't make a new FileConfiguration everytime he grabs a value, the only IO will be the initial loading of the FileConfiguration and whenever he saves it.
     
  5. Offline

    xTrollxDudex

    xTigerRebornx
    Correct, although I do believe that getConfigurationSection is likely a node lookup in a gigantic HashMap. And I would imagine the getInts probably lookup a seperate node collection...
     
  6. Offline

    xTigerRebornx

    xTrollxDudex It'd be smarter for him to go with Lore to make it based on the ItemStack and not the ItemStack's type (unless he wants it to be global for that type)
    padili
     
  7. Offline

    padili

    Thanks for your answers! I'm going to read it out now using the cached config file.
     
  8. Offline

    garbagemule

    Premature optimization is the root of all evil. Pick the most readable or simple solution you can, and optimize it if necessary. HotSpot does a lot of work behind the scenes that can make inefficient-looking code run lightning fast. Don't underestimate it, and spend your time and energy on more important matters.

    That said, there are a couple of different ways to approach this, depending on your requirements (your description is obnoxiously vague).
    • If the damage is based solely on the type of the item, use an EnumMap to map the Material to a wrapper object holding the information you need. Store this information in a config-file and read it once when the plugin is enabled. EnumMaps are lightning fast and extremely space efficient, even when compared to everyone's darling, the HashMap.
    • If the damage is based solely on the contents of a String in the name of an item, use a regular HashMap to map the name to a wrapper (like before). Again, use a config-file and read it once.
    • If the damage is based on a combination of the name and dynamically changing lore values, you have two options. Either strip the information you need on every hit, or listen for the events that change the lore values, and update your wrapper objects accordingly. If items with the same name can have different values, you need a way to distinguish them, e.g. by mapping player names to items, and thus creating an extra layer of lookups.
    Again, before embarking on a complex solution, make sure you actually need to do it. If so, make sure to profile both (or all) solutions properly (Java benchmarking is not as simple as two calls to System.currentTimeMillis() due to HotSpot and various kinds of JVM overhead). It could be that the simple solution based on String operations is actually faster than anything else you can come up with. Big-O notation is rarely useful for situations like this.
     
Thread Status:
Not open for further replies.

Share This Page