Why u no lik me?

Discussion in 'Spigot Plugin Development' started by NaJated, May 4, 2015.

  1. hi, may name's static and i wus wunderin why evewryong is spredin rumors that i slow code down? plox anser me.
     
  2. *pssst* I heard this guy called static slows code down
     
  3. that was what you were made 4
     
  4. stwop it, you war mawking postin on spigot rweally hard becus i havwe to use getters evwerywhere!?!?!
     
  5. Static is actually faster.
     
  6. Are you talking about the static modifier?

    Slows code? Wat? If it is, it's probably smaller than a nanosecond.
     
  7. faster to code but some peeps say it slows code down idk hahaha
     
    • Optimistic Optimistic x 2
  8. 1 sec, lemme just fetch my latest project :)

    Code (Text):
    package me.TheGamesHawk2001.AFK;

    import org.bukkit.Bukkit;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    public class F {
        public static String black = "§0";
        public static String darkBlue = "§1";
        public static String darkGreen = "§2";
        public static String darkAqua = "§3";
        public static String darkRed = "§4";
        public static String darkPurple = "§5";
        public static String gold = "§6";
        public static String gray = "§7";
        public static String darkGray = "§8";
        public static String blue = "§9";
        public static String green = "§a";
        public static String aqua = "§b";
        public static String red = "§c";
        public static String lightPurple = "§d";
        public static String yellow = "§e";
        public static String white = "§f";
        public static String reset = "§r";
        public static String magic = "§k";
        public static String underline = "§n";
        public static String strikethrough = "§m";
        public static String bold = "§l";
        public static String italic = "§o";
        public static String blackRule = "§0§m----------------------------------------------";
        public static String darkBlueRule = "§1§m----------------------------------------------";
        public static String darkGreenRule = "§2§m----------------------------------------------";
        public static String darkAquaRule = "§3§m----------------------------------------------";
        public static String darkRedRule = "§4§m----------------------------------------------";
        public static String darkPurpleRule = "§5§m----------------------------------------------";
        public static String goldRule = "§6§m----------------------------------------------";
        public static String grayRule = "§7§m----------------------------------------------";
        public static String darkGrayRule = "§8§m----------------------------------------------";
        public static String blueRule = "§9§m----------------------------------------------";
        public static String greenRule = "§a§m----------------------------------------------";
        public static String aquaRule = "§b§m----------------------------------------------";
        public static String redRule = "§c§m----------------------------------------------";
        public static String lightPurpleRule = "§d§m----------------------------------------------";
        public static String yellowRule = "§e§m----------------------------------------------";
        public static String whiteRule = "§f§m----------------------------------------------";
        public static String rule = "§m----------------------------------------------";
        public static String blackEquals = "§0§m==============================================";
        public static String darkBlueEquals = "§1§m==============================================";
        public static String darkGreenEquals = "§2§m==============================================";
        public static String darkAquaEquals = "§3§m==============================================";
        public static String darkRedEquals = "§4§m==============================================";
        public static String darkPurpleEquals = "§5§m==============================================";
        public static String goldEquals = "§6§m==============================================";
        public static String grayEquals = "§7§m==============================================";
        public static String darkGrayEquals = "§8§m==============================================";
        public static String blueEquals = "§9§m==============================================";
        public static String greenEquals = "§a§m==============================================";
        public static String aquaEquals = "§b§m==============================================";
        public static String redEquals = "§c§m==============================================";
        public static String lightPurpleEquals = "§d§m==============================================";
        public static String yellowEquals = "§e§m==============================================";
        public static String whiteEquals = "§f§m==============================================";
     
        public static String AFK_PREFIX =  F.gold + F.bold +  "AFK" + F.gray + ": ";
        public static void send(Player p, String msg, Sound sound){
            p.sendMessage(msg);
            p.playSound(p.getLocation(), sound, 10, 1);
        }
        @SuppressWarnings("deprecation")
        public static void broadcast(String msg){
            for(Player everyPlayer : Bukkit.getOnlinePlayers()){
                everyPlayer.sendMessage(msg);
            }
        }
        /**
         * @param  player the player that should receive the message
         * @param  msg the message the player should receive
         * @param  rule the rule at the start and end of the message
         */
        public static void sendRule(Player player, String msg, String rule){
            player.sendMessage(rule);
            player.sendMessage("");
            player.sendMessage(msg);
            player.sendMessage("");
            player.sendMessage(rule);
        }
    }
    ily <3
     
  9. That's just dirty as fuck. The ChatColor enum exists.

    Using it you could get rid of all but one rule and equals.
     
    • Agree Agree x 2
  10. It is actually slower even though it is only slower by less then a nanosecond.
    + the terrible naming conventions
     
    • Agree Agree x 1
  11. I guess you didn't catch onto the sense of irony and sarcasm haha.
     
  12. you can also dynamically generate those rule and equals at the runtime with parameters of your choice and you can cut down the number of lines significantly.

    Remember, if you don't write a code, you won't have a bug ;) try to reduce the amount of code. (to OP)
     
    • Agree Agree x 1

  13.  
  14. na, i think that code represents who you are ;)
     
  15. gigosaurus

    Supporter

    In regards to the OP:

    This is a great example of premature optimization.

    In terms of speed, static methods and variables can actually be faster in some cases (dynamic lookup is obviously going to be slower than being able to directly access something), but even then it's not going to be a noticeable difference, so making something static just because you can or because you want to increase performance, is not a valid reason.

    Static objects quite often have a larger overhead though, as they will only be collected by the GC when the class loader that loaded the static object is collected itself (which might only happen when the server is stopped). This is probably why people think they are "slower" - because they hog memory.
     
    #15 gigosaurus, May 4, 2015
    Last edited: May 4, 2015
  16. ?? you cannot be certain how GC behave. None of Java specification (language, and JVM) specify how GC should be implemented including what sort of algorithms they should be using. It is indeed very difficult to say which is faster since these days Java will go through at least two stages of optimisation (at compile time, and at the runtime through JIT/Hotspot etc runtime compiler)...
     
  17. gigosaurus

    Supporter

    I should have said static objects, not variables, after all, the GC collects objects, not references.

    It is true that you can't be certain of how the GC behaves, but one thing you can be certain about it is that it won't collect an object that is currently referenced somewhere. This means you can be certain that static objects won't be collected until the class loader that loaded the class containing the static object is collected (because that class loader, loosely speaking, is referencing the object). Of course however, you can't be certain of when the class loader is collected (if at all), but with a plugin I doubt this is before the server shuts down.
     
  18. Trust me, it isn't slower. It's faster even though it's not noticeable it is faster.
     
  19. proof?