1

this is just a question out of curiousity. Let's say, I have an application where some objects are created dynamically during runtime. And let's also say, that in most cases the number of objects won't surpass a certain treshhold, e.g. 20. And as a last precondition we say, that it is totally important to optimize the performance.

What would be the better-performing alternative?

  1. First create an array[20]. When adding objects, perform a check whether the array is already totally occupied, and if so create a new array with newArray[array.length * f], where f is a float greater than 1.0f. Then replace the old array with the new one and add the item

  2. simply use an ArrayList from the beginning

  3. ???

Remember, this is totally about performance optimization.

edit I dunno the exact implementation in java, so it might be true, that 1.) and 2.) are quite similar. But after reading this: https://stackoverflow.com/a/10747397/1075211, I think that it might make a difference in C# or some other languages?

Community
  • 1
  • 1
keinabel
  • 1,002
  • 3
  • 15
  • 33
  • Which language are you asking about, Java? – Bergi Apr 29 '13 at 17:40
  • This is the wrong way to go about performance optimization. If you have a hypothesis about a way to make your program run faster, **measure it.** – Matt Ball Apr 29 '13 at 17:41
  • Isn't 1) just the same as 2) with an ArrayList initialized with 20? – Bergi Apr 29 '13 at 17:42
  • If you are talking about Java, I think Points. 1 and 2 are the same. ArrayList already works the way your point no. 1 works. Ultimately, as mentioned by @MattBall you need to measure it to see what works for your use case. – VikrantY Apr 29 '13 at 17:43

1 Answers1

1

Your first option is more or less equivalent to creating an ArrayList like new ArrayList(20).

bobby
  • 2,629
  • 5
  • 30
  • 56