|
-
Aug 7th, 2005, 09:17 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Which is more efficient?
Hello to all,
Just want to hear your ideas on which of the following code is more efficient.
VB Code:
Dim TotalUnits as Double
' some codes here ...
Try
Units += ds.Tables(0).Rows(i).Item("UNITS")
Catch ex As Exception
' Set Units to zero if error occur.
Units = 0
Finally
TotalUnits += Units
End Try
or using if statement.
VB Code:
' Check if the column UNITS is NULL
If IsDBNull(ds.Tables(0).Rows(i).Item("UNITS")) Then
Units = 0
Else
Units = ds.Tables(0).Rows(i).Item("UNITS")
End If
' Add to TotalUnits variable.
TotalUnits += Units
Thanks.
Begin with the end in mind.
My Profile
while( !( succeed = try() ) );
-
Aug 7th, 2005, 09:34 PM
#2
Re: Which is more efficient?
I would go for the 2nd one, if you expect some errors in your code then I think it is proper to handle it yourself rather than your error handler.
-
Aug 7th, 2005, 10:07 PM
#3
Thread Starter
Addicted Member
Re: Which is more efficient?
Ok, I got your point.
Isn't that it spend more time validating everytime is passes the IF statement even without an error?? Wheras the Try Catch Block, it executes as a regular statement and falls only on the catch block if error occurs...
Though im expecting an error but the error often occurs.
This is just my opinion.
But right now, im using the try catch block... But im taking consideration your suggestion.
Begin with the end in mind.
My Profile
while( !( succeed = try() ) );
-
Aug 7th, 2005, 10:21 PM
#4
Re: Which is more efficient?
Well, I have made it a habit that if I can expect errors in my code then I try to make remedies in it before they happen.
For example is accessing a certain file, if the file doesnt exists then there will be an exception but beforehand one could already check it the file exists at all before they try to access it.
-
Aug 7th, 2005, 11:17 PM
#5
Re: Which is more efficient?
The general rule is that you NEVER use exceptions to control program flow if it can reasonably avoided. In real time, throwing the exception would take several orders of magnitude longer than the miniscule time it would take to check for a null value. Also, if you want to avoid Runtime functions you could use either of these, the first of which is probably the most efficient way possible to handle this operation:
VB Code:
If TypeOf ds.Tables(0).Rows(i).Item("UNITS") Is DBNull Then
Units = 0
Else
Units = ds.Tables(0).Rows(i).Item("UNITS")
End If
or
VB Code:
If ds.Tables(0).Rows(i).Item("UNITS") = DBNull.Value Then
Units = 0
Else
Units = ds.Tables(0).Rows(i).Item("UNITS")
End If
-
Aug 7th, 2005, 11:26 PM
#6
Thread Starter
Addicted Member
-
Aug 7th, 2005, 11:48 PM
#7
Re: Which is more efficient?
 Originally Posted by tsungik
Please mark the thread as resolved properly using "Thread Tools" so the we can see it's solved before we click into it.
-
Aug 7th, 2005, 11:52 PM
#8
Thread Starter
Addicted Member
Re: [RESOLVED] Which is more efficient?
Sorry for that Phill64.
Thread already marked.
Begin with the end in mind.
My Profile
while( !( succeed = try() ) );
-
Aug 8th, 2005, 03:43 AM
#9
Re: [RESOLVED] Which is more efficient?
You shouldn't worry about speed of execution here because it looks like database accessing. The DB is by far the slowest link in the chain and will drown out any perceivable perfornmance gain of using IF. You should still use If as said earlier. Only because its better practice to do so and not from a performance point of view.
I don't live here any more.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|