File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Performance when using objects in a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Performance when using objects in a loop" Watch "Performance when using objects in a loop" New topic
Author

Performance when using objects in a loop

Kelly Michaels
Greenhorn

Joined: Oct 18, 2010
Posts: 14
I have a situation where I could use an object for a loop in two different ways.
The second way would be:
I thought the first method was more efficient, but the second one runs faster!?! Which one is the better way? Or is it the "it depends" case?
Any thoughts would be greatly appreciated. Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16479
    
    2

The two pieces of code do different things, so there is no point in comparing them. So neither of them is "better", or more exactly the one which does what you want is "better". Not to mention that it's quite likely you have made one of the many errors typically made by beginners when writing benchmarks and got the wrong answer.
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2925
    
  15

As said by Paul, It all depends on your implementation. Benchmarking for smaller code-usually doesn't really yield enough results so that you can compare.

Looking at your current code- If the method dosomething() is not using any of the instance variables of the Object(in other words its execution doesn't depend on the state of the object) then you could as well make it as a static method and use the class to invoke that.

Frankly speaking, I haven't really thought about the performance of the application while developing it. You can atleast make sure that you aren't creating unnecessary objects, closing the file resources, sql connections after using them, then try to reduce the loop iterations- by thinking of an alternate approach.

Mohamed Sanaulla | My Blog
Kelly Michaels
Greenhorn

Joined: Oct 18, 2010
Posts: 14
Thanks Guys. I think I understand the problem better now.
Kelly Michaels
Greenhorn

Joined: Oct 18, 2010
Posts: 14
Mohamed, Just when I thought I understood Java a tiny bit, you've got me thinking again :- ) In the following example, I am calling static methods from a non-static method using an instance variable.Before your post, I would have created non-static methods for staticMethods 1 and 2 and I would have instantiated objects for them in the main class before using their methods. But if I make them static, I can get away without creating objects for staticclasses 1 and 2.

What I am trying to understand is when to use instance methods and when to use static methods. If I can get away with static methods, then I don't have to create objects. I would sincerely appreciate your feedback. Thanks!
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2925
    
  15

With static methods you cannot access the class's instance variables. And static methods are per class- So if you want some instance specific behavior then you would have to use Instance methods. Instance specific behavior means- Using the instance variable values to perform some operation.
Kelly Michaels
Greenhorn

Joined: Oct 18, 2010
Posts: 14
Thank you. After I wrote the post, I did that exactly. I created an instance variable inside the static class and tried to access it from within the static method and it wouldn't let me do it.

So If I need methods, that is instance variable dependent, then creating a static method is out of the question :- )
 
I agree. Here's the link: http://jrebel.com/download
 
subject: Performance when using objects in a loop
 
Similar Threads
Help with Assignment 1.5 - Times
Problem Using toString method from a different class
Using toArray(Object[]) in java 5
Scope of variable declarartions in for loop
for loop with one parameter