Hallo Leute so habe ich dieses Stück CodeJava padding Leistung Zerschlagung
public class Padding {
static class Pair {
volatile long c1;
// UN-comment this line and see how perofmance is boosted * 2
// long q1; //Magic dodo thingy
volatile long c2;
}
static Pair p = new Pair();
static class Worker implements Runnable {
private static final int INT = Integer.MAX_VALUE/8;
private boolean b;
Worker(boolean b) {
this.b = b;
}
public void run() {
long start = System.currentTimeMillis();
if (b) {
for (int i = 0; i < INT; i++) {
p.c1++;
res += Math.random();
}
} else {
for (int i = 0; i < INT; i++) {
p.c2++;
res += Math.random();
}
}
long end = System.currentTimeMillis();
System.out.println("took: " + (end-start) + " Result:" + p.c1+p.c2);
}
}
public static void main(String[] args) {
System.out.println("Starting....");
Thread t1 = new Thread(new Worker(true));
Thread t2 = new Thread(new Worker(false));
t1.start();
t2.start();
}
}
Also, wenn ich es dauert etwa 11 Sekunden laufen, aber wenn ich qa1 die Auskommentierung es läuft in 3 Sekunden .I versucht um etwas im Internet zu finden, aber nichts Aufschlussreiches kam auf. Wie ich es verstehe, hat es etwas mit JVM-Optimierung zu tun und lange Q1 macht wahrscheinlich Speicher (oder Cache) Verteilung, wie besser. Wie auch immer meine Frage ist, weiß jemand, wo ich mehr darüber lesen kann. Dank
Wo beziehst du die Variable 'q1'? – user2004685
Wo deklarieren Sie 'res'? –
Das ist die Thihng Sie nicht derzeit ist es in Zeile 4 auskommentiert, aber wenn Kommentar es die magische hapens – urag