|
|
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.
|
|

05-28-2008, 04:39 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 1
|
|
|
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
|
|

05-28-2008, 05:17 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
|
|
|
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
|
|

05-28-2008, 05:27 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,543
|
|
|
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.
|
|

05-28-2008, 05:31 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
|
|
|
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
|
|

05-28-2008, 05:47 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,543
|
|
|
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.
|
|

05-28-2008, 05:53 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
|
|
|
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
|
|

05-28-2008, 05:58 AM
|
 |
Member
|
|
Join Date: Apr 2008
Location: State College, PA
Posts: 50
|
|
|
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.
|
|

05-28-2008, 06:03 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
|
|
|
Most compilers will optimsize your if statment. Mostly what you want to consider is readability.
__________________
My IP address is 127.0.0.1
|
|

05-28-2008, 06:43 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,543
|
|
Originally Posted by Zosden
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.
|
|

05-28-2008, 12:49 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 526
|
|
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.
|
|

05-28-2008, 02:43 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
|
|
|
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
|
|

05-28-2008, 03:14 PM
|
 |
Member
|
|
Join Date: Apr 2008
Location: State College, PA
Posts: 50
|
|
|
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.
|
|

05-28-2008, 10:32 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 21
|
|
|
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.
|
|

05-29-2008, 01:11 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 1
|
|
Its always good programming practice to use the:
if (rs != null){rs.close();}

|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|