Performance Question

Discussion in 'Plugin Development' started by pkt, May 5, 2013.

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

    pkt

    Is it faster to use hashmaps or save to file?
    For Example:
    I want to keep track of a players health. should I:
    1. use a hashmap to store it, then save it to file when the player leaves.
    OR
    2. save it directly to a file everytime the players health changes

    I'm thinking 1, but I would like to know if anyone has any facts or statistics they can show me or explain
     
  2. Offline

    HackintoshMan

    Probably a hash map. That saves to the ram which is much faster than saving to the disk. But don't forget to save and load the hash map on enable and disableā€¦
     
  3. Offline

    MP5K

    Hello pkt,
    i think it will be a lot faster if you cache the Data (so use a HashMap)
    (but remember never cache a Player Object)
     
    teunie75 likes this.
  4. Offline

    pkt

    Exactly what I was thinking!
     
  5. pkt
    If you got a number as value, a mutable number would be even faster because you'll be looking it up only once and avoid putting it back in the map since you're mutably modifying the value.
    You should google about mutable and immutable objects if you don't know what I'm talking about... but here's an example:
    Code:
    Map<String, MutableInt> map = new HashMap<String, MutableInt>();
    
    // ...
    
    MutableInt data = map.get(player.getName());
    
    if(data == null)
    {
        data = new MutableInt(0);
        map.put(player.getName(), data); // only put it once if it doesn't exist
    }
    
    data.add(1); // add 1 to the value of the stored integer, this will directly affect the value of the map because it's a mutable object, you can change its contents without creating a new object
    This method does not limit to numbers, you can just create a class, create some fields for it to store stuff in it and add to the map, then just get the object and change it.
     
Thread Status:
Not open for further replies.

Share This Page