db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ilkka Priha <impr...@surfeu.fi>
Subject Re: Vector use
Date Tue, 15 Jun 2004 08:56:09 GMT
Synchronization overhead is surprisingly low, at least with the newest Java VMs, 
in non-concurrent situations, but can cause bottlenecks in concurrent ones, 
which really slows down performance. On the other hand, if concurrency is 
expected, collections can't be substituted with non-synchronized classes without 
resolving the normal issues of thread-safety.

As this topic has come up in almost all of our Java projects, we've made some 
simple benchmarks with ordered Integer objects to test (only) synchronization of 
collections. All collections have been constructed with enough space to hold the 
test objects (150% with maps) to avoid reallocation and rehashing. GC is called 
just before each test case.

With integer based over-simplified hashcodes Hashtable and Vector perform 
actually better than the corresponding HashMap and ArrayList because their put, 
get and remove methods are somewhat simpler and call less internal methods. 
Synchronization of HashMap with Collections.syncronizedMap cause an overhead 
between 2-7 % depending on the operation. Synchronization of ArrayList with 
Collections.syncronizedList cause quite high relative overhead but it's only 
about 10 ns per call. Other benchmarks have shown that in more realistic hashing 
situations HashMap should perform better then Hashtable but probably because of 
better rehashing than missing synchronization.

Sun JRE 1.4.2_004, Windows XP Pro, AMD Athlon XP 2400+, 1024 MB
---------------------------------------------------------------
INFO [15/Jun/2004:08:03:20 +0000] Hashtable put 5000000 times: 		1324
INFO [15/Jun/2004:08:03:20 +0000] Hashtable get 5000000 times: 		468
INFO [15/Jun/2004:08:03:21 +0000] Hashtable remove 5000000 times: 	452
INFO [15/Jun/2004:08:03:27 +0000] HashMap put 5000000 times: 		3849
INFO [15/Jun/2004:08:03:29 +0000] HashMap get 5000000 times: 		2508
INFO [15/Jun/2004:08:03:33 +0000] HashMap remove 5000000 times: 	3397
INFO [15/Jun/2004:08:03:39 +0000] Synced HashMap put 5000000 times: 	3942
INFO [15/Jun/2004:08:03:42 +0000] Synced HashMap get 5000000 times: 	2680
INFO [15/Jun/2004:08:03:45 +0000] Synced HashMap remove 5000000 times: 	3459
INFO [15/Jun/2004:08:03:47 +0000] Vector add 5000000 times: 		203
INFO [15/Jun/2004:08:03:47 +0000] Vector get 5000000 times: 		171
INFO [15/Jun/2004:08:03:47 +0000] Vector remove 5000000 times: 		156
INFO [15/Jun/2004:08:03:49 +0000] ArrayList add 5000000 times: 		172
INFO [15/Jun/2004:08:03:49 +0000] ArrayList get 5000000 times: 		187
INFO [15/Jun/2004:08:03:49 +0000] ArrayList remove 5000000 times: 	171
INFO [15/Jun/2004:08:03:50 +0000] Synced ArrayList add 5000000 times: 	218
INFO [15/Jun/2004:08:03:51 +0000] Synced ArrayList get 5000000 times: 	234
INFO [15/Jun/2004:08:03:51 +0000] Synced ArrayList remove 5000000 times: 530

With multi-processor hardware the synchronization takes more time because the 
underlying mechanism is much more complicated. We've run benchmarks only in a 
two processor machine. Even there Hashtable performs better than HashMap, but 
Vector is now clearly slower than ArrayList (but still faster than synchronized 
ArrayList).

Windows 2000 Server, 2 x Intel PIII 1266 MHz, 512 MB
---------------------------------------------------------------------
INFO [15/Jun/2004:08:15:12 +0000] Hashtable put 5000000 times: 3172
INFO [15/Jun/2004:08:15:13 +0000] Hashtable get 5000000 times: 953
INFO [15/Jun/2004:08:15:14 +0000] Hashtable remove 5000000 times: 969
INFO [15/Jun/2004:08:15:22 +0000] HashMap put 5000000 times: 5813
INFO [15/Jun/2004:08:15:25 +0000] HashMap get 5000000 times: 2437
INFO [15/Jun/2004:08:15:28 +0000] HashMap remove 5000000 times: 3594
INFO [15/Jun/2004:08:15:37 +0000] Synced HashMap put 5000000 times: 6485
INFO [15/Jun/2004:08:15:40 +0000] Synced HashMap get 5000000 times: 2922
INFO [15/Jun/2004:08:15:44 +0000] Synced HashMap remove 5000000 times: 4234
INFO [15/Jun/2004:08:15:47 +0000] Vector add 5000000 times: 688
INFO [15/Jun/2004:08:15:47 +0000] Vector get 5000000 times: 593
INFO [15/Jun/2004:08:15:48 +0000] Vector remove 5000000 times: 579
INFO [15/Jun/2004:08:15:51 +0000] ArrayList add 5000000 times: 313
INFO [15/Jun/2004:08:15:51 +0000] ArrayList get 5000000 times: 296
INFO [15/Jun/2004:08:15:51 +0000] ArrayList remove 5000000 times: 344
INFO [15/Jun/2004:08:15:54 +0000] Synced ArrayList add 5000000 times: 735
INFO [15/Jun/2004:08:15:54 +0000] Synced ArrayList get 5000000 times: 703
INFO [15/Jun/2004:08:15:55 +0000] Synced ArrayList remove 5000000 times: 719

-- Ilkka


Thomas Dudziak wrote:
> Robert Sfeir wrote:
> 
>> I've been wondering about the use of Vector and Hashtable in OJB.  Are 
>> they there because we support older JDKs or is it because it's just 
>> what was there when OJB was first developed?  Vector and Hashtables 
>> are deprecated, and newer Collections are just more efficient.
> 
> 
> Actually this is not true, they are not at all deprecated but rather 
> retrofitted into the new collection strategy (implementing Map/List). 
> There are also subtle but important differences for instance between 
> Hashtable and HashMap: Hashtable is synchronized (possibly slower but 
> safer) and does not allow to use null as key. Vector is also 
> synchronized (unlike ArrayList) but the implementation can be faster 
> (depending on the JDK).
> 
>> I don't mind going through weeding them out, but if we intend to 
>> support JDK 1.2 and before then there is no sense in doing that.  Is 
>> there a large group of OJB users using JDK 1.2- ?
> 
> 
> I wouldn't move away from that because the more interesting classes were 
> introduced in 1.4 and a lot of people still use 1.3 JDKs/JREs.
> If there are spots were a replacement of Vector/Hashtable makes sense 
> (justified by performance measurements and with unit tests as 
> safety-net) then I'd rather replace them with commons-collections than 
> with JDK 1.4.
> 
> Tom
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-dev-help@db.apache.org
> 
> .
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message