• Post Reply Bookmark Topic Watch Topic
  • New Topic

Performance when using objects in a loop  RSS feed

 
Kelly Michaels
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kelly Michaels
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys. I think I understand the problem better now.
 
Kelly Michaels
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :- )
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!