Solved Is this efficient?

Discussion in 'Plugin Development' started by MrBlackIsBack, Jul 10, 2015.

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

    MrBlackIsBack

    Hey guys!

    I made a nice handy method that takes a string and, makes it rainbow coloured! But, I'm not sure if it is efficient as I plan to release the class when I'm finished with it! Here is the method:

    Code:
    public String rainbow(String msg)
        {
            // &1 &2 &3 &4 &5 &6 &7 &8 &9 &0 | 10 / 9
    
            char[] c = msg.toCharArray();
    
            String newMsg = "";
    
            int color = 1;
            for (char ch : c)
            {
                newMsg += trans("&" + color + ch);
                color++;
    
                if (color == 9)
                    color = 1;
            }
    
            return newMsg;
        }
    Thanks in advance!

    (I don't post a lot of threads on bukkit, so I'm not sure if this is the right section)
     
  2. Offline

    _Error

    Can't find a way to make it more efficient, I'd say you've done a good job.
     
    MrBlackIsBack likes this.
  3. Offline

    MrBlackIsBack

  4. Offline

    teej107

    It's not the most efficient. Use a StringBuilder to concatenate Strings together. Also if it was me, I wouldn't add the ChatColors like you do. I would just use the ChatColor enum. You can get an Array of enum values with #values() for enum.
     
  5. Offline

    mythbusterma

    Really, the question you should ask here is, "does the performance of this method produce a measurable/noticeable performance hit?"

    As a great man once said, "premature optimisation is the root of all evil."
     
Thread Status:
Not open for further replies.

Share This Page