Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local member is faster or instance member

Tags:

java

jvm

The following code proves that method1 is faster than method2. Can anyone please comment what is the reason for such behavior.

class Trial {
        String _member;
        void method1() {
            for(int i=0;i<30480;i++) {
                _member += "test";
            }
        }
        void method2() {
            String temp="";
            for(int i=0;i<30480;i++) {
                temp += "test";
            }
            _member = temp;
        }

        public static void main(String args[]) {
            Trial t = new Trial();
            long startTime1 = System.currentTimeMillis();
            t.method1();
            long endTime1 = System.currentTimeMillis();
            long startTime2 = System.currentTimeMillis();
            t.method2();
            long endTime2 = System.currentTimeMillis();
            System.out.println(endTime1 - startTime1);
            System.out.println(endTime2 - startTime2);
        }
    }
like image 655
Amber Avatar asked Jun 18 '13 07:06

Amber


People also ask

What is the difference between static members and instance members?

The best approach to this, in my opinion, is to think of static members as members which belong to a class and instance members as members which belong to the object itself. We can also distinguish three types of members. Let's start with the classes.

What is an instance member in Java?

In other words, when we talk about instance members, we talk about those members of a class that cannot be called or accessed without instantiating the class. Those that can be accessed are called static members. Let’s look at a static class. 1 The value of the Frozen class is 99 2 Says the Frozen class: Why would you do this?

Why is the local version of a class faster?

The local version is ~33% faster while still part of a class. Why? Show activity on this post. self.m += 1 means you have to look up a local variable called self and then find the attribute called m Of course if you just have to look up a local variable, it will be faster without the extra step.

What are the types of members in a class?

When we are working with classes we have two types of members, either static or instance members. The best approach to this, in my opinion, is to think of static members as members which belong to a class and instance members as members which belong to the object itself. We can also distinguish three types of members. Let's start with the classes.


1 Answers

The following code proves that method1 is faster than method2

No. It does not prove it.

It depends on many factors. When I run this code, I get

1403
1248

So in my environment, your code "proves" that method1 is slower than method2.

When doing benchmarking, you need to take care of effects like caching and JVM warmup.

See also

  • How do I write a correct micro-benchmark in Java?
  • Avoid jvm warmup

for more information.


I slightly refactored the main method:

...

static void doBenchmark() {
   Trial t = new Trial();

   long startTime1 = System.currentTimeMillis();
   t.method1();
   long endTime1 = System.currentTimeMillis();

   long startTime2 = System.currentTimeMillis();
   t.method2();
   long endTime2 = System.currentTimeMillis();

   System.out.println(endTime1 - startTime1);
   System.out.println(endTime2 - startTime2);
}

public static void main(String args[]) {

   for (int i = 0;  i < 20;  i++) {
      doBenchmark();
      System.out.println("----");
   }
}

This results in similar values for the first iteration of the for-loop, but then the results converge and do not differ significantly anymore:

1396
1133
----
1052
1070
----
688
711
----
728
726
----
715
709
----
...

Even, sometimes method1 seems faster and sometimes method2 - this is most likely due to measurement inaccuracy.

like image 144
Andreas Fester Avatar answered Oct 21 '22 15:10

Andreas Fester