aspose file tools*
The moose likes Beginning Java and the fly likes Efficiency of Each Java Operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Efficiency of Each Java Operation" Watch "Efficiency of Each Java Operation" New topic
Author

Efficiency of Each Java Operation

Jai Gates
Greenhorn

Joined: Jul 11, 2012
Posts: 18
Does anyone know of a good resource/s that could tell me the efficiency of individual operation in Java


I don't know whether to put a try-catch block in a loop or vice versa....



It would help to have a list of operations and their resource overhead.



Since I'm such a beginner I might just get this thing to work albeit inefficiently so I can get the thing in on time. ..but it bothers me.


-J
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60975
    
  65

You should be worrying about making your code clear and readable. Efficiency, especially down to the individual operation level, is not something you should be thinking of.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jai Gates
Greenhorn

Joined: Jul 11, 2012
Posts: 18
Ok thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60975
    
  65

Jai Gates wrote:I don't know whether to put a try-catch block in a loop or vice versa....

You choose the one that makes the most sense.

It would help to have a list of operations and their resource overhead.

Nope. Not something that should be a factor in deciding how to write your code.

Learn good and efficient algorithms as you go along, yes. But worrying about the efficiency of individual operations, or worse, writing contorted code because you think it will be "more efficient", will definitely steer you wrong.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jai Gates wrote:Does anyone know of a good resource/s that could tell me the efficiency of individual operation in Java


No, and you don't need it. The "efficiency" at the very low level is something you can't control and that will be handled differently by different JVM implementations and versions. If you want your program to run efficiently, use good design and appropriate algorithms and data structures.

I don't know whether to put a try-catch block in a loop or vice versa....


You won't make that choice for "efficiency" reasons. You'll make it for design and functionality reasons. If you want your entire loop to stop when one exception occurs, put the loop inside the try. If you want to be able to recover from an exception on a given iteration and continue looping, put the try inside the loop.

Write code that says what you mean. Don't try to be "clever" in the name of "efficiency."

Since I'm such a beginner I might just get this thing to work albeit inefficiently so I can get the thing in on time. ..but it bothers me.


Good performance will come from things like using Sets to find an intersection of collections, rather than an O(N^2) nested for loop over two Lists. You won't see a difference from things like the relative location of your loops and try statements. For good performance, write dumb code!

Jai Gates
Greenhorn

Joined: Jul 11, 2012
Posts: 18
For good performance, write dumb code!



Advice I can follow xD thanks for article. will read.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7633
    
  19

Jai Gates wrote:Advice I can follow xD thanks for article. will read.

And, just in case you're still tempted, my favourite quotation about optimization:
W.A. Wulf wrote:"More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason — including blind stupidity."


Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Efficiency of Each Java Operation