• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList - adding - initialCapacity

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I read:

The default initial capacity of an ArrayList is pretty small (10 from Java 1.4 - 1.7). But since the underlying implementation is an array, the array must be resized if you add a lot of elements. To avoid the high cost of resizing when you know you're going to add a lot of elements, construct the ArrayList with a higher initial capacity.



For checking this I developed this test. Where I create two ArraList objects. First one with default initial capacity (10) and the other with the number of elements I will add (LIMIT_1). Also I test the time that requiere each adding.



Here the output:


The results are on the contrary I expected. The one with resizing is slower.
Any suggestion?


Thanks,
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just tested your code using eclipse Juno and JDK 1.7.0_40. My output is:

ArrayList1 add: 22804238
ArrayList2 add: 9089667
 
Jose Rguez
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So confusing. I am using also: Eclipse IDE for Java Developers. Version: Juno Service Release 1.
I am glad to see what I thought was right and this only happen in my computer. But what a mistery!

Thanks,
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've even ran it a few more times:

ArrayList1 add: 14388636
ArrayList2 add: 9186677

ArrayList1 add: 16920900
ArrayList2 add: 9920025

ArrayList1 add: 15204365
ArrayList2 add: 9220168
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Elier Rguez wrote:So confusing. I am using also: Eclipse IDE for Java Developers. Version: Juno Service Release 1.
I am glad to see what I thought was right and this only happen in my computer. But what a mistery!

Thanks,


How many times did you run it?
 
Jose Rguez
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested many times with similar results.

But now I reduced a little de number of iterations:


and I get a expected result;


I think the problem could come from compilator configuration, maybe it could be the heap size.


Thanks,
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change the order of computation and see what results you get. What results do you get if the first list is the one in which you have specified the capacity.

Also if you run both of these computations separately multiple times, do you see a difference?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic