Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-28-2008, 04:39 AM
Member
 
Join Date: May 2008
Posts: 1
sathishranganathan is on a distinguished road
Best practice of IF loop with NOT
Hi,

Please suggest me the best practice of using if loop with 'NOT' condition.

For example:
if (rs != null){rs.close();}

if (rs == null){} else{rs.close();}

Which of the above is optimised?

Thanks in advance

Sathish
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-28-2008, 05:17 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
depends on compiler but you probably won't notice any difference in speed even if you run it a trillion times.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-28-2008, 05:27 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,543
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
In most of the cases, each line of a code effect to the execution completion time. If you can remove at least one line of code, it's much better.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-28-2008, 05:31 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
This true in only some case Eranga. Think of inlining your methods although this adds more code it also makes your program faster because of the lack of method calls. Another example would be to hardcode in a inner loop for an array search.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-28-2008, 05:47 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,543
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes, I mentioned on my replay pal. Please read what I have written there carefully before comments on it.

And you know what, array search is much faster than calling a method. If you not believe me do a simple JUnit test and see.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-28-2008, 05:53 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
I'm confused by what you mean. Arrays were never mentioned in this thread nor where method calls
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-28-2008, 05:58 AM
Chris.Brown.SPE's Avatar
Member
 
Join Date: Apr 2008
Location: State College, PA
Posts: 50
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
I do agree that the execution time between them is negligible, but it is just good practice to use the not statement. You don't want to have an empty block. It may have to go into the block and then see that it is empty which would take time, but compilers should optimize around that.

I would think that it would be faster to use a try/catch block on the rs.close, then it wouldnt have to do anything extra unless the file was null which i am assuming isnt going to happen very often.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-28-2008, 06:03 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
Most compilers will optimsize your if statment. Mostly what you want to consider is readability.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-28-2008, 06:43 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,543
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by Zosden View Post
Think of inlining your methods although this adds more code it also makes your program faster because of the lack of method calls. Another example would be to hardcode in a inner loop for an array search.
If so I'm misunderstand on this.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-28-2008, 12:49 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 526
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Quote:
Please suggest me the best practice of using if loop with 'NOT' condition.

For example:
if (rs != null){rs.close();}

if (rs == null){} else{rs.close();}

Which of the above is optimised?
You may code it in favor to what you are expecting....

The first one may be faster than the second one, only if that portion always executes successfully and should be close,

What if rs has 51% chance to be null? therefore the second one is faster than the first one....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by sukatoa : 05-28-2008 at 12:52 PM.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-28-2008, 02:43 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
I do believe but I could be wrong that with a smart compiler it would take the exact amount of time either way.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 05-28-2008, 03:14 PM
Chris.Brown.SPE's Avatar
Member
 
Join Date: Apr 2008
Location: State College, PA
Posts: 50
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
I agree. I would expect the java compiler to be smart enough to get those both compiled well. This would be different with an interpreted language.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 05-28-2008, 10:32 PM
Member
 
Join Date: May 2008
Posts: 21
gogoc is on a distinguished road
it is best to avoid "NOT" other then "NOT EQUALS" as it will take an extra conversion to convert the result to its not
and more over try to inscribe less executed code int the loop if u are going for multiple condition and using && in between vice-versa
__________________
get new coding problems at
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 05-29-2008, 01:11 PM
Member
 
Join Date: May 2008
Posts: 1
superwaxer is on a distinguished road
Its always good programming practice to use the:

if (rs != null){rs.close();}

Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use Do While loop Java Tip java.lang 0 04-17-2008 08:45 PM
How to use While loop Java Tip java.lang 0 04-17-2008 08:44 PM
Building small web application in java for practice. Saurabh321 New To Java 1 02-01-2008 04:38 PM
I need help on a practice code for java Sageinquisitor New To Java 10 07-17-2007 12:23 AM
Web Services - IBM Expands SOA Management Practice Felissa Web Frameworks 0 06-25-2007 05:08 AM


All times are GMT +3. The time now is 01:16 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org