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?

4
  • Which language are you asking about, Java? Commented Apr 29, 2013 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. Commented Apr 29, 2013 at 17:41
  • Isn't 1) just the same as 2) with an ArrayList initialized with 20? Commented Apr 29, 2013 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. Commented Apr 29, 2013 at 17:43

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

this goes for java, but also for other languages?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.