File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes Java Performance Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Java Performance Issue" Watch "Java Performance Issue" New topic

Java Performance Issue

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

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.

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
I don't have any java performance evaluation tool with me. Are there any open source tools available for this?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
10 is such a small number for modern computers that it hardly matters at all which approach you are using.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Java Performance Issue
It's not a secret anymore!