Static discussion

Discussion in 'Plugin Development' started by xTrollxDudex, Dec 17, 2014.

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

    xTrollxDudex

    While it is very high performance:
    [​IMG]
    (it is very difficult to see, but static manages to stay slightly under instance).

    It is not object oriented, the margin is tiny and should be used for special cases like factories and utility classes.

    Benchmark (not perfect, JMH is not working for me):
    Code:
    class StaticBenchmark {
        private int instance = 0;
        private static int stat = 0;
    
        private static final int MEASURE = 100_000_000;
        private static final int WARM = 1_000;
        private static final StaticBenchmark inst = new StaticBenchmark();
    
        public static void main (String[] args) throws Exception {
            // Warmup
            for (int i = 0; i < WARM; i++) {
                inst.doInstance();
                StaticBenchmark.doStatic();
            }
    
            for (int i = 0; i < WARM; i++) {
                inst.doInstance();
                StaticBenchmark.doStatic();
            }
    
            // Measurement
            for (int i = 0; i < 10; i++) {
                testInstance();
            }
    
            for (int i = 0; i < 10; i++) {
                testStatic();
            }
        }
    
        public static void testInstance() {
            long begin = System.nanoTime();
            int iterations = 0;
            do {
                inst.doInstance();
                iterations++;
            } while (iterations < MEASURE);
            System.out.println("Instance " + (double) (System.nanoTime() - begin) / MEASURE);
        }
    
        public static void testStatic() {
            long begin = System.nanoTime();
            int iterations = 0;
            do {
                doStatic();
                iterations++;
            } while (iterations < MEASURE);
            System.out.println("Static " + (double) (System.nanoTime() - begin) / MEASURE);
        }
    
        public void doInstance() {
            instance++;
        }
    
        public static void doStatic() {
            stat++;
        }
    }
     
    Last edited by a moderator: Dec 17, 2014
  2. Offline

    mythbusterma

    @xTrollxDudex

    That difference is quite literally nanoscopic and shouldn't really be considered in any discussions, at all. I can't think of any usage case at all where such a difference would manifest itself, and even if there exists such a case, that code should be implemented in native calls anyway.
     
    Rocoty likes this.
  3. Offline

    xTrollxDudex

    @mythbusterma
    Native calls have JNI overhead, in many cases, it has a slower performance than normal Java implementations. For example, the new keyword is faster than malloc in C. Another case is with Unsafe class, using a native lock method is slower than using synchronized.
     
  4. Offline

    mythbusterma

    @xTrollxDudex

    True, but overall the call would be faster if you were doing any amount of work that would make this difference noticeable.
     
  5. Offline

    RawCode

    Already discussed multiple times.
    There is no reason to ever think about static vs instance performance.

    some not very smart authors of youtube "tutorials" spread this rumor and now it haunt forum.
     
    mythbusterma and Skionz like this.
  6. Offline

    xize

    Its interesting to see this, but I think whilst static its a bad habbit even when static is may a little bit faster, if it stores for every class a anonymous copy rather than passing it through I think static does definatly more bad in performance than what a non static Object would do being passed through constructors.
     
  7. Offline

    Rocoty

    One word: micro-optimisation
     
  8. Offline

    xTrollxDudex

  9. Offline

    mythbusterma

    This post brought a tear to my eye, it is so beautiful.
     
    Funergy, Avygeil and Rocoty like this.
Thread Status:
Not open for further replies.

Share This Page