[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.
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. :)
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.
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.
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
Re: Which is more efficient?
:thumb: :thumb: :thumb:
Thanks for the input. "Thread Resolved"
Re: Which is more efficient?
Quote:
Originally Posted by tsungik
:thumb: :thumb: :thumb:
Thanks for the input. "Thread Resolved"
Please mark the thread as resolved properly using "Thread Tools" so the we can see it's solved before we click into it.
Re: [RESOLVED] Which is more efficient?
Sorry for that Phill64.
Thread already marked.
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.