Solved Is this efficient?

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

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


    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:

    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);
                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


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


  4. Offline


    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


    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