2 Efficiency Questions

Discussion in 'Plugin Development' started by macguy8, May 15, 2013.

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

    macguy8

    Hello! I have 2 questions on efficiency of calls (they're mainly Java things, not Bukkit)

    1) Which is faster...
    Code:
    if (something()) {
      doSomething();
    } else {
      doSomethingElse();
    }
    
    or
    Code:
    if (something()) {
      doSomething();
      return;
    }
     
    doSomethingElse();
    }
    
    2) Which is faster...
    Static getter or passing the instance of a plugin.
    I know passing the instance is sort of a hastle, and the JVM has to sort of "determine" where to run the method, due to classes extending one another.
    I've heard static is generally slow, but compared to the hastle and maybe a **small** small performance increase, is there any "better" way of doing it?

    Thanks![/code]
     
  2. Offline

    Icyene

    1) They are absolutely identical in terms of the generated bytecode. Don't bother with such minor things, you'll never, ever see a significant improvement in speed, even if there was one. Premature optimization is the root of all evil.

    2) Static getter, or even better (in terms of readability), static public member. Yet once again, there is no discernible difference: static public member might even be faster due to the lack of a method invocation.

    But really, the JVM is not slow. There is no reason why you should ever need to optimize such things. A method invocation, static or not, takes under a few nanoseconds. The JVM was slow before the introduction of the JIT, but that outdated information no longer holds now; almost 8 years later.

    /rant
     
    KingFaris11, Bone008 and macguy8 like this.
  3. Offline

    macguy8

    Icyene
    Alright. Thanks Icyene for your great response.
     
Thread Status:
Not open for further replies.

Share This Page