Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36896

Re: A third easy performance optimisation needed

Newsgroups comp.lang.java.programmer
Date 2017-07-13 08:07 -0700
References <87d1946ab3.fsf@munus.decebal.nl> <ok7oar$1uaf$2@gioia.aioe.org> <cf0341d5-fc31-4aca-822e-d51c5d74587f@googlegroups.com> <87pod44foe.fsf@munus.decebal.nl> <99effbd1-7b12-4b43-8086-952d3557ae5e@googlegroups.com>
Message-ID <8a5bf72f-66dd-4042-9c22-0fadcc416e6d@googlegroups.com> (permalink)
Subject Re: A third easy performance optimisation needed
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


On Thursday, July 13, 2017 at 10:10:06 AM UTC-4, Eric Douglas wrote:
> 
> I could be wrong but it looks to me that your test code here is significantly flawed.  It was brought up in that other thread that the test was flawed.
> 

So here's a good example of flawed testing.  Eric S posted a link to a blog about finals.  Someone in the comments section posted a sample they claim is proof that code runs faster with variables declared as final.
I pasted their text into Eclipse, added a zero to iterations to get a better sample size, and hit run.

Test 1)

public class FinalTest {

public static final int N_ITERATIONS = 100000000;

public static String testNonFinal() {
String a = "a";
String b = "b";
return a + b;
}

public static String testFinal() {
final String a = "a";
final String b = "b";
return a + b;
}

public static void main(String[] args) {
long tStart;

tStart = System.currentTimeMillis();
for (int i = 0; i < N_ITERATIONS; i++)
testFinal();
System.out.println("Method with finals took " + (System.currentTimeMillis() - tStart) + " ms");

tStart = System.currentTimeMillis();
for (int i = 0; i < N_ITERATIONS; i++)
testNonFinal();
System.out.println("Method without finals took " + (System.currentTimeMillis() - tStart) + " ms");
}
}

Result: It claims the non-final method takes 500-540 ms and the final method takes 0-16 ms.

Test 2: I reverse the order to see if that mattered on optimization.


public static void main(String[] args) {
long tStart;

tStart = System.currentTimeMillis();
for (int i = 0; i < N_ITERATIONS; i++)
testNonFinal();
System.out.println("Method without finals took " + (System.currentTimeMillis() - tStart) + " ms");

tStart = System.currentTimeMillis();
for (int i = 0; i < N_ITERATIONS; i++)
testFinal();
System.out.println("Method with finals took " + (System.currentTimeMillis() - tStart) + " ms");
}
}

Result: It claims a method with final variables takes 0-16 ms and the same method without finals takes 500-540 ms.

Test 3: I move a variable outside the limited little method.

public class FinalTest {

public static final int N_ITERATIONS = 100000000;

public static String testNonFinal(String a) {
String b = "b";
return a + b;
}

public static String testFinal(final String a) {
final String b = "b";
return a + b;
}

public static void main(String[] args) {
long tStart;

tStart = System.currentTimeMillis();
for (int i = 0; i < N_ITERATIONS; i++)
testNonFinal("a");
System.out.println("Method without finals took " + (System.currentTimeMillis() - tStart) + " ms");

tStart = System.currentTimeMillis();
for (int i = 0; i < N_ITERATIONS; i++)
testFinal("a");
System.out.println("Method with finals took " + (System.currentTimeMillis() - tStart) + " ms");
}
}

Result: It claims this method takes around 430 ms with finals, or around 3300 without.

Test 4: I reverse the order.


public static void main(String[] args) {
long tStart;

tStart = System.currentTimeMillis();
for (int i = 0; i < N_ITERATIONS; i++)
testFinal("a");
System.out.println("Method with finals took " + (System.currentTimeMillis() - tStart) + " ms");

tStart = System.currentTimeMillis();
for (int i = 0; i < N_ITERATIONS; i++)
testNonFinal("a");
System.out.println("Method without finals took " + (System.currentTimeMillis() - tStart) + " ms");
}
}

Now it claims the method takes around 3300 ms with finals, around 430 ms without finals.  I changed the passed in value from "a" to a final variable, same result.

Conclusion: Tests can be deceiving.  Make sure you've tested every angle before you preach something as fact.  If you think something makes your code faster, does it make all code faster?  Why does it seem faster?

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

A third easy performance optimisation needed Cecil Westerhof <Cecil@decebal.nl> - 2017-07-13 09:54 +0200
  Re:A third easy performance optimisation needed omegajunior <omegajunior@protonmail.com> - 2017-07-13 06:29 -0400
  Re: A third easy performance optimisation needed Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-13 11:27 +0000
  Re: A third easy performance optimisation needed Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-13 07:41 -0400
    Re: A third easy performance optimisation needed Eric Douglas <e.d.programmer@gmail.com> - 2017-07-13 05:48 -0700
      Re: A third easy performance optimisation needed Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 19:39 -0400
    Re: A third easy performance optimisation needed Cecil Westerhof <Cecil@decebal.nl> - 2017-07-13 15:14 +0200
      Re: A third easy performance optimisation needed Eric Douglas <e.d.programmer@gmail.com> - 2017-07-13 07:07 -0700
        Re: A third easy performance optimisation needed Robert Klemme <shortcutter@googlemail.com> - 2017-07-13 19:58 +0200
          Re: A third easy performance optimisation needed Cecil Westerhof <Cecil@decebal.nl> - 2017-07-13 20:02 +0200
  Re: A third easy performance optimisation needed Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 08:16 -0400
    Re: A third easy performance optimisation needed Eric Douglas <e.d.programmer@gmail.com> - 2017-07-13 06:08 -0700
      Re: A third easy performance optimisation needed Cecil Westerhof <Cecil@decebal.nl> - 2017-07-13 15:41 +0200
        Re: A third easy performance optimisation needed Eric Douglas <e.d.programmer@gmail.com> - 2017-07-13 07:09 -0700
          Re: A third easy performance optimisation needed Eric Douglas <e.d.programmer@gmail.com> - 2017-07-13 08:07 -0700
          Re: A third easy performance optimisation needed Cecil Westerhof <Cecil@decebal.nl> - 2017-07-13 18:37 +0200
        Re: A third easy performance optimisation needed Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2017-07-13 13:19 -0500
          Re: A third easy performance optimisation needed Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 14:32 -0400
      Re: A third easy performance optimisation needed Arne Vajhøj <arne@vajhoej.dk> - 2017-07-13 13:52 -0400
        Re: A third easy performance optimisation needed Eric Douglas <e.d.programmer@gmail.com> - 2017-07-14 07:52 -0700
  A third easy performance optimisation needed lewbloch@gmail.com - 2017-07-14 14:47 -0700

csiph-web