Created
October 9, 2013 08:24
-
-
Save karl82/6898012 to your computer and use it in GitHub Desktop.
F:\>java -version<br>
java version "1.6.0_06"<br>
Java(TM) SE Runtime Environment (build 1.6.0_06-b02)<br>
Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)<br>
<br>
SynchronizationTest1:<br>
client<br>
Cumulative time for testX(): 51,702,940 ns<br>
Cumulative time for testY(): 616,381,691 ns<br>
Cumulative time for testZ(): 619,2…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Jednoduchy benchmark pro porovnani rychlostnich | |
* rozdilu mezi pouzitim synchronizovanych metod | |
* a synchronizovanych bloku. | |
*/ | |
public class SynchronizationTest1 { | |
// dostatecny pocet iteraci pro spusteni JITu | |
private final static int ITERATIONS = 5000000; | |
// zamezime tomu, aby se do casu behu benchmarku | |
// zapocital i cas JITu | |
private final static int WARMUP = 3; | |
public int x = 0; | |
public int y = 0; | |
public int z = 0; | |
public volatile int v = 0; | |
// test s nesynchronizovanou metodou | |
private void testX() { | |
x++; | |
} | |
// test se synchronizovanou metodou | |
private synchronized void testY() { | |
y++; | |
} | |
// test s metodou se synchronizovanym blokem | |
private void testZ() { | |
synchronized(this) { | |
z++; | |
} | |
} | |
// test s metodou vyuzivajici volatilni atribut | |
private void testV() { | |
v++; | |
} | |
// spusteni benchmarku | |
public static void main(String[] args) { | |
SynchronizationTest1 test = new SynchronizationTest1(); | |
long t1, t2, delta_t; | |
long sumTestX=0, sumTestY=0, sumTestZ=0, sumTestV=0; | |
// provest zadany pocet testu | |
for (int i = 0; i < 10; i++) { | |
// provest test a zmerit cas behu testu | |
t1 = System.nanoTime(); | |
for (int j = 0; j < ITERATIONS; j++) { | |
test.testX(); | |
} | |
t2 = System.nanoTime(); | |
delta_t = t2 - t1; | |
if (i > WARMUP) { | |
sumTestX += delta_t; | |
} | |
// vypis casu pro prvni test | |
System.out.format("Round #%2d testX() time: %,12d ns\n", i, delta_t); | |
// provest test a zmerit cas behu testu | |
t1 = System.nanoTime(); | |
for (int j = 0; j < ITERATIONS; j++) { | |
test.testY(); | |
} | |
t2 = System.nanoTime(); | |
delta_t = t2 - t1; | |
if (i > WARMUP) { | |
sumTestY += delta_t; | |
} | |
// vypis casu pro druhy test | |
System.out.format("Round #%2d testY() time: %,12d ns\n", i, delta_t); | |
// provest test a zmerit cas behu testu | |
t1 = System.nanoTime(); | |
for (int j = 0; j < ITERATIONS; j++) { | |
test.testZ(); | |
} | |
t2 = System.nanoTime(); | |
delta_t = t2 - t1; | |
if (i > WARMUP) { | |
sumTestZ += delta_t; | |
} | |
// vypis casu pro treti test | |
System.out.format("Round #%2d testZ() time: %,12d ns\n", i, delta_t); | |
// provest test a zmerit cas behu testu | |
t1 = System.nanoTime(); | |
for (int j = 0; j < ITERATIONS; j++) { | |
test.testV(); | |
} | |
t2 = System.nanoTime(); | |
delta_t = t2 - t1; | |
if (i > WARMUP) { | |
sumTestV += delta_t; | |
} | |
// vypis casu pro treti test | |
System.out.format("Round #%2d testV() time: %,12d ns\n", i, delta_t); | |
} | |
// vypis vsech ctyr kumulativnich casu | |
System.out.format("Cumulative time for testX(): %,12d ns\n", sumTestX); | |
System.out.format("Cumulative time for testY(): %,12d ns\n", sumTestY); | |
System.out.format("Cumulative time for testZ(): %,12d ns\n", sumTestZ); | |
System.out.format("Cumulative time for testV(): %,12d ns\n", sumTestV); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Jednoduchy benchmark pro porovnani rychlostnich | |
* rozdilu mezi pouzitim synchronizovanych metod | |
* a synchronizovanych bloku. | |
*/ | |
public class SynchronizationTest2 { | |
// dostatecny pocet iteraci pro spusteni JITu | |
private final static int ITERATIONS = 5000000; | |
// zamezime tomu, aby se do casu behu benchmarku | |
// zapocital i cas JITu | |
private final static int WARMUP = 3; | |
long sumTestX=0, sumTestY=0, sumTestZ=0, sumTestV=0; | |
public int x = 0; | |
public int y = 0; | |
public int z = 0; | |
public volatile int v = 0; | |
// test s nesynchronizovanou metodou | |
private void testX() { | |
x++; | |
} | |
// test se synchronizovanou metodou | |
private synchronized void testY() { | |
y++; | |
} | |
// test s metodou se synchronizovanym blokem | |
private void testZ() { | |
synchronized(this) { | |
z++; | |
} | |
} | |
// test s metodou vyuzivajici volatilni atribut | |
private void testV() { | |
v++; | |
} | |
// spusteni benchmarku | |
public static void main(String[] args) { | |
SynchronizationTest2 test = new SynchronizationTest2(); | |
long t1, t2, delta_t; | |
// provest zadany pocet testu | |
for (int i = 0; i < 10; i++) { | |
test.round(i); | |
} | |
test.printResults(); | |
} | |
private void printResults(){ | |
// vypis vsech ctyr kumulativnich casu | |
System.out.format("Cumulative time for testX(): %,12d ns\n", sumTestX); | |
System.out.format("Cumulative time for testY(): %,12d ns\n", sumTestY); | |
System.out.format("Cumulative time for testZ(): %,12d ns\n", sumTestZ); | |
System.out.format("Cumulative time for testV(): %,12d ns\n", sumTestV); | |
} | |
private void round(int i) { | |
long t1; | |
long t2; | |
long delta_t;// provest test a zmerit cas behu testu | |
t1 = System.nanoTime(); | |
for (int j = 0; j < ITERATIONS; j++) { | |
testX(); | |
} | |
t2 = System.nanoTime(); | |
delta_t = t2 - t1; | |
if (i > WARMUP) { | |
sumTestX += delta_t; | |
} | |
// vypis casu pro prvni test | |
System.out.format("Round #%2d testX() time: %,12d ns\n", i, delta_t); | |
// provest test a zmerit cas behu testu | |
t1 = System.nanoTime(); | |
for (int j = 0; j < ITERATIONS; j++) { | |
testY(); | |
} | |
t2 = System.nanoTime(); | |
delta_t = t2 - t1; | |
if (i > WARMUP) { | |
sumTestY += delta_t; | |
} | |
// vypis casu pro druhy test | |
System.out.format("Round #%2d testY() time: %,12d ns\n", i, delta_t); | |
// provest test a zmerit cas behu testu | |
t1 = System.nanoTime(); | |
for (int j = 0; j < ITERATIONS; j++) { | |
testZ(); | |
} | |
t2 = System.nanoTime(); | |
delta_t = t2 - t1; | |
if (i > WARMUP) { | |
sumTestZ += delta_t; | |
} | |
// vypis casu pro treti test | |
System.out.format("Round #%2d testZ() time: %,12d ns\n", i, delta_t); | |
// provest test a zmerit cas behu testu | |
t1 = System.nanoTime(); | |
for (int j = 0; j < ITERATIONS; j++) { | |
testV(); | |
} | |
t2 = System.nanoTime(); | |
delta_t = t2 - t1; | |
if (i > WARMUP) { | |
sumTestV += delta_t; | |
} | |
// vypis casu pro treti test | |
System.out.format("Round #%2d testV() time: %,12d ns\n", i, delta_t); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Jednoduchy benchmark pro porovnani rychlostnich | |
* rozdilu mezi pouzitim synchronizovanych metod | |
* a synchronizovanych bloku. | |
*/ | |
public class SynchronizationTest3 { | |
// dostatecny pocet iteraci pro spusteni JITu | |
private final static int ITERATIONS = 5000000; | |
// zamezime tomu, aby se do casu behu benchmarku | |
// zapocital i cas JITu | |
private final static int WARMUP = 3; | |
long sumTestX=0, sumTestY=0, sumTestZ=0, sumTestV=0; | |
public int x = 0; | |
public int y = 0; | |
public int z = 0; | |
public volatile int v = 0; | |
// test s nesynchronizovanou metodou | |
private void testX() { | |
x++; | |
} | |
// test se synchronizovanou metodou | |
private synchronized void testY() { | |
y++; | |
} | |
// test s metodou se synchronizovanym blokem | |
private void testZ() { | |
synchronized(this) { | |
z++; | |
} | |
} | |
// test s metodou vyuzivajici volatilni atribut | |
private void testV() { | |
v++; | |
} | |
// spusteni benchmarku | |
public static void main(String[] args) { | |
SynchronizationTest3 test = new SynchronizationTest3(); | |
long t1, t2, delta_t; | |
// provest zadany pocet testu | |
for (int i = 0; i < 10; i++) { | |
test.round(i); | |
} | |
test.printResults(); | |
} | |
private void printResults(){ | |
// vypis vsech ctyr kumulativnich casu | |
System.out.format("Cumulative time for testX(): %,12d ns\n", sumTestX); | |
System.out.format("Cumulative time for testY(): %,12d ns\n", sumTestY); | |
System.out.format("Cumulative time for testZ(): %,12d ns\n", sumTestZ); | |
System.out.format("Cumulative time for testV(): %,12d ns\n", sumTestV); | |
} | |
private void round(int i) { | |
doTestX(i); | |
doTextY(i); | |
doTestZ(i); | |
doTestV(i); | |
} | |
private void doTestV(int i) { | |
long t1; | |
long t2; | |
long delta_t;// provest test a zmerit cas behu testu | |
t1 = System.nanoTime(); | |
for (int j = 0; j < ITERATIONS; j++) { | |
testV(); | |
} | |
t2 = System.nanoTime(); | |
delta_t = t2 - t1; | |
if (i > WARMUP) { | |
sumTestV += delta_t; | |
} | |
// vypis casu pro treti test | |
System.out.format("Round #%2d testV() time: %,12d ns\n", i, delta_t); | |
} | |
private void doTestZ(int i) { | |
long t1; | |
long t2; | |
long delta_t;// provest test a zmerit cas behu testu | |
t1 = System.nanoTime(); | |
for (int j = 0; j < ITERATIONS; j++) { | |
testZ(); | |
} | |
t2 = System.nanoTime(); | |
delta_t = t2 - t1; | |
if (i > WARMUP) { | |
sumTestZ += delta_t; | |
} | |
// vypis casu pro treti test | |
System.out.format("Round #%2d testZ() time: %,12d ns\n", i, delta_t); | |
} | |
private void doTextY(int i) { | |
long t1; | |
long t2; | |
long delta_t;// provest test a zmerit cas behu testu | |
t1 = System.nanoTime(); | |
for (int j = 0; j < ITERATIONS; j++) { | |
testY(); | |
} | |
t2 = System.nanoTime(); | |
delta_t = t2 - t1; | |
if (i > WARMUP) { | |
sumTestY += delta_t; | |
} | |
// vypis casu pro druhy test | |
System.out.format("Round #%2d testY() time: %,12d ns\n", i, delta_t); | |
} | |
private void doTestX(int i) { | |
long t1; | |
long t2; | |
long delta_t; | |
t1 = System.nanoTime(); | |
for (int j = 0; j < ITERATIONS; j++) { | |
testX(); | |
} | |
t2 = System.nanoTime(); | |
delta_t = t2 - t1; | |
if (i > WARMUP) { | |
sumTestX += delta_t; | |
} | |
// vypis casu pro prvni test | |
System.out.format("Round #%2d testX() time: %,12d ns\n", i, delta_t); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment