Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Performance issuse  RSS feed

 
Saman Perera
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

this pro is somewhat performance issuse

public class NewTest
{

public NewTest()
{
super();
// TODO Auto-generated constructor stub
}
// method 1
public ArrayList getArrayList(){

ArrayList arrayList = new ArrayList(0);
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
return arrayList;
}
// method 2
public ArrayList getArrayListWithPara(ArrayList arrayList){

arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
return arrayList;
}


public static void main(String[] args)
{
NewTest newTest = new NewTest();
ArrayList arrayList = newTest.getArrayList();
// use arrayList
arrayList.clear();
ArrayList arrayListWithPara = new ArrayList(0);
arrayListWithPara =newTest.getArrayListWithPara(arrayListWithPara);
// use arrayListWithPara
arrayListWithPara.clear();
}
}


i want to know what is the best possible method " method 1" or "method 2", there is no diffreence


thanks all
saman
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saman, but which J2EE Technology are you using? It actually looks like a question that might be better suited for the Java In General (Beginner) forum. I will move this thread to that forum for you.

Thanks

Mark
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems like an inappropriate stage at which to be asking the question based on performance. Right now those methods do two similar but different things, they are not performing the same task in different ways but in fact performing different tasks entirely.

In a very limited context I would say the first method is slower. You create an ArrayList with an initial capacity of zero then add three elements. That alone is going to force the ArrayList to resize it's internal array three times. Furthermore you are forced to create a new ArrayList for every invocation of the method.

In the second you get passed the ArrayList as a parameter and simply add to it. This could be faster or it could be just as slow depending entirely on how the client uses it. If I myself create a new ArrayList with a capacity of zero to pass to that method every time I invoke it then it's going to be just as slow. However, if I'm reusing an ArrayList that is already of sufficient capacity it will probably be faster.

None of this, however, is particularly relevent. You need to address what your requirements are and how you will use this API before you start worrying about what the most performant way of accomplishing it is. What is it you are trying to do? Or is this just purely an academic question?
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList list = new ArrayList()

This creates an empty list with a capacity of 10. Passing 0 as an argument to the constructor is very inefficient.

Each time you add an element, the ArrayList should not be automatically resized. If it resized three times when three items were inserted, that would be a very inefficient algorithm. I couldn't find specific details on the growth algorithm so I might be walking off the edge a bit here. But if they do increase the size after every insertion, it will be the first implementation of a resizable array that I have seen that does this. Since the docs say the time cost is constant, I assume that this is also not the case with ArrayList.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html

I think the second method would be a bit faster, and better to use. It just makes more sense to me. You could always put in a timing mechanism into main and time them. Do get any sort of result you would like have to put each method call in a loop and do it 1000 times or so and find the average. I doubt either one would take more then 10ms to execute, which would be too fast to measure in a windows enviroment, hence the need for the loops.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rusty Shackleford:
ArrayList list = new ArrayList()

This creates an empty list with a capacity of 10. Passing 0 as an argument to the constructor is very inefficient.

Each time you add an element, the ArrayList should not be automatically resized. If it resized three times when three items were inserted, that would be a very inefficient algorithm. I couldn't find specific details on the growth algorithm so I might be walking off the edge a bit here. But if they do increase the size after every insertion, it will be the first implementation of a resizable array that I have seen that does this. Since the docs say the time cost is constant, I assume that this is also not the case with ArrayList.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html

I think the second method would be a bit faster, and better to use. It just makes more sense to me. You could always put in a timing mechanism into main and time them. Do get any sort of result you would like have to put each method call in a loop and do it 1000 times or so and find the average. I doubt either one would take more then 10ms to execute, which would be too fast to measure in a windows enviroment, hence the need for the loops.


The array is resized before the insertion if it's necessary. With an initial capacity of 0 the first three insertions will require a resize. The exact algorithm is:

(capacity * 3) / 2 + 1

Remember that these are all int variables and will truncate and you'll see why the first three insertions will all require a resize.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!