Hallo zusammen,
Ich habe mich gefragt ob Java automatisch a = b im Falle a == b nicht ausführt.
Ich habe mich gefragt ob Java automatisch a = b im Falle a == b nicht ausführt.
public static void main(String[] args) {
int a = 5;
int b = 5;
for(int i=0; i<10000; i++) {
a = b;
}
System.out.println(a);
}
0: iconst_5
1: istore_1
2: iconst_5
3: istore_2
4: iconst_0
5: istore_3
6: iload_3
7: sipush 10000
10: if_icmpge 21
13: iload_2
14: istore_1
15: iinc 3, 1
18: goto 6
21: getstatic #2
24: iload_1
25: invokevirtual #3
28: return
public static void main(String[] args) {
for (int i = 0; i < 1000000; i++) {
doSomeStuff();
}
}
public static int doSomeStuff() {
int a = 5;
int b = 5;
a = b;
return a;
}
# {method} {0x000000001b8c0310} 'doSomeStuff' '()I' in 'Main'
# [sp+0x40] (sp of caller)
0x0000000004ab5f60: mov dword ptr [rsp+0ffffffffffffa000h],eax
0x0000000004ab5f67: push rbp
0x0000000004ab5f68: sub rsp,30h ;*iconst_5
; - Main::doSomeStuff@0 (line 13)
0x0000000004ab5f6c: mov eax,5h
0x0000000004ab5f71: add rsp,30h
0x0000000004ab5f75: pop rbp
0x0000000004ab5f76: test dword ptr [5e0100h],eax ; {poll_return}
0x0000000004ab5f7c: ret
0x0000000004ab5f7d: nop
0x0000000004ab5f7e: nop
0x0000000004ab5f7f: mov rax,qword ptr [r15+2a8h]
0x0000000004ab5f86: mov r10,0h
0x0000000004ab5f90: mov qword ptr [r15+2a8h],r10
0x0000000004ab5f97: mov r10,0h
0x0000000004ab5fa1: mov qword ptr [r15+2b0h],r10
0x0000000004ab5fa8: add rsp,30h
0x0000000004ab5fac: pop rbp
0x0000000004ab5fad: jmp 4a0cae0h ; {runtime_call}
0x0000000004ab5fb2: hlt
0x0000000004ab5fb3: hlt
0x0000000004ab5fb4: hlt
0x0000000004ab5fb5: hlt
0x0000000004ab5fb6: hlt
0x0000000004ab5fb7: hlt
0x0000000004ab5fb8: hlt
0x0000000004ab5fb9: hlt
0x0000000004ab5fba: hlt
0x0000000004ab5fbb: hlt
0x0000000004ab5fbc: hlt
0x0000000004ab5fbd: hlt
0x0000000004ab5fbe: hlt
0x0000000004ab5fbf: hlt
public static int doSomeStuff(int i) {
int a = 1;
int b = i % 2;
a = b;
return a+b;
}
Auch wenn man natürlich nicht mit dem Vorsatz programmieren sollte, dass die JVM schon alles wegoptimiert .
Brian Goetz hat gesagt.:Often, the way to write fast code in Java applications is to write dumb code -- code that is straightforward, clean, and follows the most obvious object-oriented principles