Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Java Performance Issue  RSS feed

 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Myself and my friend are having an issue with which approach to follow which gives us a better performance. Here are the scenarios

scenario - I

Instantiate an ArrayList and add 10 string objects to it.

scenario - II

Create a user defined object with a project called name with getter/setter method and instantiate the same class 10 times and set the name property with unique names.

I felt approach I is better in terms of performance. But my friend is saying since we are adding string objects to the arraylist it doesn't make any difference in performance since all are objects.

I felt instantiating the same object 10 times explicitly, is a performance hit when compared to adding string objects to arrylist.

I want to confirm which one is the right approach.
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you write a small program to test both scenarios? Just write a small program that performs each of the two scenarios in a loop many times and measure which one is faster.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have any java performance evaluation tool with me. Are there any open source tools available for this?
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.currentTimeMillis()?
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
10 is such a small number for modern computers that it hardly matters at all which approach you are using.
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aside from performance, do you need to keep the data around, or not? It seems to me that if you create 10 objects (which is not a big deal, as Ilja says) you still probably want to put them in a List of some sort (or maybe an array) - don't you? Or if you don't need a List, then you probably didn't need a List of Strings either. It seems like there are two separate, unrelated questions here:

1. Do you want to represent the data using only Strings, or using objects of some custom class that contains a name field?

2. Do you want to put multiple objects in a List, or an array, or can you just throw each object away after you're done with it?

As usual, performance seems to be a relatively unimportant part of the problem here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!