Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Clear Arraylist vs new ArrayList (http://www.velocityreviews.com/forums/t617210-clear-arraylist-vs-new-arraylist.html)

Philipp 05-28-2008 07:22 AM

Clear Arraylist vs new ArrayList
 
Hello,
What is the most efficient and preferred way to clear a List in terms of
speed and memory?

list = new ArrayList();
or
list.clear();

clear() walks all elements and sets them to null, maybe this is not
efficient for large lists?

Phil

Andrea Francia 05-28-2008 07:37 AM

Re: Clear Arraylist vs new ArrayList
 
Philipp wrote:
> Hello,
> What is the most efficient and preferred way to clear a List in terms of
> speed and memory?


This seems a premature optimization question.
Google for these terms.

Do you measured the needs to speed up you program and do you discovered
that the problem is the array creation?

>
> list = new ArrayList();
> or
> list.clear();
>
> clear() walks all elements and sets them to null, maybe this is not
> efficient for large lists?


The best way is the more readable way.
When you finish to create your program and after you measured that there
are some bottleneck with a profiler you can solve them.

>
> Phil



--
Andrea Francia
http://andreafrancia.blogspot.com/

Arne Vajh°j 05-28-2008 02:56 PM

Re: Clear Arraylist vs new ArrayList
 
Philipp wrote:
> What is the most efficient and preferred way to clear a List in terms of
> speed and memory?
>
> list = new ArrayList();
> or
> list.clear();
>
> clear() walks all elements and sets them to null, maybe this is not
> efficient for large lists?


Probably the new is slightly faster.

But I am pretty sure that it does not matter for your app, so
it is the wrong question.

The correct question is: does creating a new list or removing
the elements from the existing lists best model the real world
your program is simulating ?

Arne

Andreas Leitgeb 05-28-2008 03:47 PM

Re: Clear Arraylist vs new ArrayList
 
Arne Vajh°j <arne@vajhoej.dk> wrote:
> Philipp wrote:
>> What is the most efficient and preferred way to clear a List in terms of
>> speed and memory?
>> list = new ArrayList();
>> or
>> list.clear();


>> clear() walks all elements and sets them to null,


I'd rather think that the internal array that keeps
the refs is dumped on .clear(), without any walk-through.

> The correct question is: does creating a new list or removing
> the elements from the existing lists best model the real world
> your program is simulating ?


E.g. if you pass (a reference to) an ArrayList to another method,
then it makes a *lot* of difference, whether you .clear() the
collection or whether you replace it with a new one within the
called method.


Patricia Shanahan 05-28-2008 05:03 PM

Re: Clear Arraylist vs new ArrayList
 
Andreas Leitgeb wrote:
> Arne Vajh°j <arne@vajhoej.dk> wrote:
>
>>Philipp wrote:
>>
>>>What is the most efficient and preferred way to clear a List in terms of
>>>speed and memory?
>>>list = new ArrayList();
>>> or
>>>list.clear();

>
>
>>>clear() walks all elements and sets them to null,

>
>
> I'd rather think that the internal array that keeps
> the refs is dumped on .clear(), without any walk-through.


I looked at the source code (JRE 1.5), and it does walk through the
array. It might be more efficient to do it the way you say.

That does create a significant difference between clear() and creating a
new ArrayList. The new ArrayList will start with a small array, and
build it up as needed for the elements added after the clearing. Calling
clear leaves the array at the size it was immediately before.

Using clear() might be the better choice if a lot of CPU time is going
on copying in ArrayList as the list rebuilds after the clear. On the
other hand, new might be the better choice if the ArrayList was very
long, will have fewer elements in the future, and the program is using a
lot of memory.

However, there is an important functional difference. If any copies of
the "list" variable escaped, the other holders of references will go on
seeing the old ArrayList if new is used, but will see their list drop to
zero elements after clear. I would code this however best expresses the
intent, and be very careful about changing it for performance tuniing.

Patricia

Roedy Green 05-28-2008 06:32 PM

Re: Clear Arraylist vs new ArrayList
 
On Wed, 28 May 2008 09:22:10 +0200, Philipp <sicsicsic@freesurf.ch>
wrote, quoted or indirectly quoted someone who said :

>What is the most efficient and preferred way to clear a List in terms of
>speed and memory?
>
>list = new ArrayList();
> or
>list.clear();
>
>clear() walks all elements and sets them to null, maybe this is not
>efficient for large lists?


If you need a new array the same size or slightly smaller, clear would
be preferable. If not, new.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Arne Vajh°j 05-28-2008 09:07 PM

Re: Clear Arraylist vs new ArrayList
 
Andreas Leitgeb wrote:
> Arne Vajh°j <arne@vajhoej.dk> wrote:
>> Philipp wrote:
>>> What is the most efficient and preferred way to clear a List in terms of
>>> speed and memory?
>>> list = new ArrayList();
>>> or
>>> list.clear();

>
>>> clear() walks all elements and sets them to null,

>
> I'd rather think that the internal array that keeps
> the refs is dumped on .clear(), without any walk-through.


It does loop and set to null.

>> The correct question is: does creating a new list or removing
>> the elements from the existing lists best model the real world
>> your program is simulating ?

>
> E.g. if you pass (a reference to) an ArrayList to another method,
> then it makes a *lot* of difference, whether you .clear() the
> collection or whether you replace it with a new one within the
> called method.


Absolutely.

Arne



All times are GMT. The time now is 01:11 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.