[JAVA] Hoe slim optimized JAVA final statements?

Pagina: 1
Acties:

  • TheOneLLama
  • Registratie: Oktober 2000
  • Laatst online: 20-01-2022

TheOneLLama

A llama like no llama before

Topicstarter
Stel ik heb dit stukje code..
code:
1
2
3
4
5
6
final static boolean DEBUG = false;

void functieBleh() {
  if (DEBUG) System.out.println("bla!");
  // doe zooi;
  }

In hoeverre heeft dit invloed op de performance (in vergelijking met dit statement helemaal niet in je prog zetten). Een beetje slimme C compiler haalt zoiets natuurlijk gelijk weg aangezien het inaccesable code is, maar doet JAVA dit ook als je naar bytecode compileert?

Opera OpenOffice.org Jabber Psi jabber://llama@mordax.com


  • Jelmer
  • Registratie: Maart 2000
  • Laatst online: 17:37
Uhm, volgens mij wel.. Had gisteren nl een static final int van waarde veranderd, maar ik moest echt de class die hm gebruikte geheel opnieuw compilen (dus .class file weg en dan weer compilen, want het werd niet opgemerkt).
Dus opzich is de Java compiler best slim, maar ook weer niet (kan trouwens ook aan codeguide gelegen hebben hoor) :)

  • Alarmnummer
  • Registratie: Juli 2001
  • Laatst online: 09-07-2024

Alarmnummer

-= Tja =-

java compiler ook (althans volgens artikel op javaworld).

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
De Java Compiler doet nog wel iets ;) .

Een 2 in 1 test:
code:
1
2
3
4
5
6
7
8
9
10
11
12
public class OptTest {
    private static final boolean DEBUG = false;
    private static final int     CONST = 0;

    public void blaat() {
      if(DEBUG) {
        System.out.println("Debug");
      }

      System.out.println("CONST: " + CONST);
    }
}

Je ziet hier dus een if statement met een constante false en een CONST gebruik, waarbij hopelijk constante propagatie wordt gebruikt.

We halen jad eroverheen:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   OptTest.java

import java.io.PrintStream;

public class OptTest
{

    public OptTest()
    {
    }

    public void blaat()
    {
      System.out.println("CONST: 0");
    }

    private static final boolean DEBUG = false;
    private static final int CONST = 0;
}

Netjes denk ik zo he? :) .

Zelfs de optelling met de int wordt al door de compiler gedaan 8-) .

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Nog even een leuker voorbeeld:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class OptTest {
    private static final boolean DEBUG = false;
    private static final int     CONST = 0;

    public void blaat() {
      if(DEBUG) {
        System.out.println("Debug");
      }

      if(DEBUG || CONST == 0) {
        System.out.println("CONST: " + CONST);
      }
    }
}

Jad:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   OptTest.java

import java.io.PrintStream;

public class OptTest
{

    public OptTest()
    {
    }

    public void blaat()
    {
      System.out.println("CONST: 0");
    }

    private static final boolean DEBUG = false;
    private static final int CONST = 0;
}

en nog eentje:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class OptTest {
    private static final boolean DEBUG = false;
    private static final int     CONST = 0;

    public void blaat() {
      if(DEBUG) {
        System.out.println("Debug");
      }

      if(DEBUG || CONST == 1) {
        System.out.println("CONST: " + CONST);
      }
    }
}

tada:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   OptTest.java


public class OptTest
{

    public OptTest()
    {
    }

    public void blaat()
    {
    }

    private static final boolean DEBUG = false;
    private static final int CONST = 0;
}

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • TheOneLLama
  • Registratie: Oktober 2000
  • Laatst online: 20-01-2022

TheOneLLama

A llama like no llama before

Topicstarter
Vermoede het zelf al, maar fijn om het zeker te weten.. thanx iedereen! :)

Het lijkt erop dat de JAVA compiler final ongeveer het zelfde behandeld als #include in C (zoals in dat 2e voorbeeld te zien is)..

Opera OpenOffice.org Jabber Psi jabber://llama@mordax.com

Pagina: 1