Synchronization article on developerWorks
Tom Tromey
tromey@redhat.com
Tue Jul 24 13:34:00 GMT 2001
>>>>> "Bryce" == Bryce McKinlay <bryce@waitaki.otago.ac.nz> writes:
Bryce> Here's an interesting article about the costs of
Bryce> syncronization. Especially interesting because GCJ gets
Bryce> mentioned in the performance tests:
Bryce> http://www-106.ibm.com/developerworks/java/library/j-threads1.html
I ran the appended test against gcj 3.0 and 3.1.
Each time I compiled with -O.
Then I removed `synchronized' and re-ran.
I forget offhand how my trees are built (debug or not). Maybe these
results are invalid :-(. Though really it would only affect the
hashmapGet and create tests.
3.0no 3.0sync 3.1no 3.1sync
================================================================
empty 1.0 33.0 0.82 26.7 (32.5)
staticEmpty 1.0 13.1 0.65 9.8 (15.2)
fetch 1.0 31.1 0.84 25.1 (29.9)
singleton 1.0 23.9 1.0 20.1 (20.1)
hashmapGet 1.0 2.3 1.1 2.1 ( 2.0)
create 1.0 2.1 0.86 1.8 ( 2.1)
The numbers in parens are 3.1-with-sync relative to 3.1-no-sync.
Raw data is at the end.
3.1 is generally better than 3.0 (not sure why). However, I expected
more dramatic results from 3.1-with-sync.
Tom
import java.util.*;
public class bench {
public static final int COUNT = 10000000;
public Object field;
public static Object singletonField;
public HashMap hashMap;
public synchronized static void staticEmpty() { }
public synchronized void empty() { }
public synchronized Object fetch() { return field; }
public synchronized Object singleton() {
if (singletonField == null)
singletonField = new Object();
return singletonField;
}
public synchronized Object hashmapGet() {
return hashMap.get("this");
}
public synchronized Object create() {
return new Object();
}
public bench ()
{
field = new Object ();
hashMap = new HashMap ();
hashMap.put ("this", this);
}
public void output (String what, long t1, long t2)
{
System.out.println (what + " = " + (t2 - t1));
}
public void test ()
{
long t1 = System.currentTimeMillis ();
for (int i = 0; i < COUNT; ++i)
empty ();
long t2 = System.currentTimeMillis ();
output ("empty", t1, t2);
t1 = System.currentTimeMillis ();
for (int i = 0; i < COUNT; ++i)
staticEmpty ();
t2 = System.currentTimeMillis ();
output ("staticEmpty", t1, t2);
t1 = System.currentTimeMillis ();
for (int i = 0; i < COUNT; ++i)
fetch ();
t2 = System.currentTimeMillis ();
output ("fetch", t1, t2);
t1 = System.currentTimeMillis ();
for (int i = 0; i < COUNT; ++i)
singleton ();
t2 = System.currentTimeMillis ();
output ("singleton", t1, t2);
t1 = System.currentTimeMillis ();
for (int i = 0; i < COUNT; ++i)
hashmapGet ();
t2 = System.currentTimeMillis ();
output ("hashmapGet", t1, t2);
t1 = System.currentTimeMillis ();
for (int i = 0; i < COUNT; ++i)
create ();
t2 = System.currentTimeMillis ();
output ("create", t1, t2);
}
public static void main (String[] args)
{
new bench ().test ();
}
}
gcc3.0 -O no sync
empty = 383
staticEmpty = 1074
fetch = 414
singleton = 522
hashmapGet = 9498
create = 11084
gcc3.1 -O no sync
empty = 315
staticEmpty = 695
fetch = 348
singleton = 522
hashmapGet = 10448
create = 9561
gcc3.0 -O sync
empty = 12649
staticEmpty = 14121
fetch = 12859
singleton = 12482
hashmapGet = 21892
create = 23276
gcc3.1 -O sync
empty = 10230
staticEmpty = 10573
fetch = 10402
singleton = 10493
hashmapGet = 20404
create = 19779
More information about the Java
mailing list