This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Performance and the fly likes 100 Objects or 100 Calls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Performance
Bookmark "100 Objects or 100 Calls" Watch "100 Objects or 100 Calls" New topic

100 Objects or 100 Calls

Mike Cole
Ranch Hand

Joined: Sep 25, 2006
Posts: 38
Hi all,

can someone explain me what is slower/faster or better/worse:
Haveing a list with 100 or more Objects and call a function from eachone
or or having the object once and call a function 100 Times.

(maybe you wonder how i came up with such a question?... well iam trying to find out how i should store a map like it is used by games)
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1864

Here is what I have tried via code

Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Rahul, I think Michael assumed that the 100 objects already were created,
i.e. you shouldn't create the object before you call the method.

Michal, try to perform much like the one Rahul made.

for 1..100 do list[i].perform()


for 1..100 do obj.perform()

and compare the result.

From a performance point of view, creating a single object instead of
creating 100 object is obviously to be prefeered (memory) in the general case, however
im sure there are arguments for creating 100 objects instead of having
one single object performing 100 jobs.
[ September 27, 2006: Message edited by: Svend Rost ]
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
I don't think that there will be signicicant difference (the method will run 100 times in both cases).
Mike Cole
Ranch Hand

Joined: Sep 25, 2006
Posts: 38
Hi all,
first, thank you for your efforts to help me!
Svend was right. The object creation itself is no problem... just
the function calls. But the hint about the moemory usage was good.
I didn�t think of that.... that can become a problem(sooner or later).
It looks like that i have to tryout all the possibilities.

Again thank you all!


kundan sinha

Joined: Aug 01, 2005
Posts: 15
Hi Rahul,

How are you getting these Execution Statistics ?

Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1864
Using Eclipse Test and Performance Tools
I agree. Here's the link:
subject: 100 Objects or 100 Calls
It's not a secret anymore!