Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Performance Issue

 
Vishnu Prakash
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
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?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.currentTimeMillis()?
 
Ilja Preuss
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.
 
Jim Yingst
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic