aspose file tools*
The moose likes Performance and the fly likes Pre allocating collections, maps, stringbuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Pre allocating collections, maps, stringbuffer" Watch "Pre allocating collections, maps, stringbuffer" New topic
Author

Pre allocating collections, maps, stringbuffer

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This little tester seems to tell me there's very little value in specifying the size on the constructor of the classes under test. The output has tabs making it suitable for copy-paste into Excel for graphing nearly flat lines.

Tested on Java 1.4 within WSAD 5.1.2 on laptop with 1.6 ghz, 1 gig of 797mhz memory.

Does this seem like a valid test? I thought about baking up the "entryn" strings in an array before running any tests.

Does it suggest the same thing to you?



A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
I think the value in allocating collections at the size you expect them to reach instead of letting them expand "automatically" shows up only with really large collections that strain memory management. The new array creation and copying that occurs with automatic expansion is really quite fast.

Bill
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I bet that's right. I ran this up to 100,000 entries to get it to run long enough to measure at all. It would be more interesting on a system that's already running into memory issues. I think I'm not going to bother to pre-allocate until I've seen something cause a problem.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I tried adding String concatenation to this to compare it to StringBuffer. The warmup() call never returned. I got bored after a few minutes and killed it. I ran it without the warmup and the elapsed time went up on a curve that was getting pretty steep early in the test. Killed again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pre allocating collections, maps, stringbuffer