Results 1 to 9 of 9

Thread: [RESOLVED] Which is more efficient?

  1. #1

    Thread Starter
    Addicted Member tsungik's Avatar
    Join Date
    Aug 2004
    Location
    Philippines
    Posts
    194

    Resolved [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:
    1. Dim TotalUnits as Double
    2.  
    3. ' some codes here ...
    4.  
    5. Try
    6.    Units += ds.Tables(0).Rows(i).Item("UNITS")
    7. Catch ex As Exception
    8.    ' Set Units to zero if error occur.
    9.    Units = 0
    10. Finally
    11.    TotalUnits += Units
    12. End Try

    or using if statement.

    VB Code:
    1. ' Check if the column UNITS is NULL
    2. If IsDBNull(ds.Tables(0).Rows(i).Item("UNITS")) Then
    3.    Units = 0
    4. Else
    5.    Units = ds.Tables(0).Rows(i).Item("UNITS")
    6. End If
    7. ' Add to TotalUnits variable.
    8. TotalUnits += Units

    Thanks.
    Begin with the end in mind.

    My Profile

    while( !( succeed = try() ) );

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

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


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Addicted Member tsungik's Avatar
    Join Date
    Aug 2004
    Location
    Philippines
    Posts
    194

    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() ) );

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

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


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. If TypeOf ds.Tables(0).Rows(i).Item("UNITS") Is DBNull Then
    2.    Units = 0
    3. Else
    4.    Units = ds.Tables(0).Rows(i).Item("UNITS")
    5. End If
    or
    VB Code:
    1. If ds.Tables(0).Rows(i).Item("UNITS") = DBNull.Value Then
    2.    Units = 0
    3. Else
    4.    Units = ds.Tables(0).Rows(i).Item("UNITS")
    5. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member tsungik's Avatar
    Join Date
    Aug 2004
    Location
    Philippines
    Posts
    194

    Resolved Re: Which is more efficient?



    Thanks for the input. "Thread Resolved"
    Begin with the end in mind.

    My Profile

    while( !( succeed = try() ) );

  7. #7
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Which is more efficient?

    Quote Originally Posted by tsungik


    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.

  8. #8

    Thread Starter
    Addicted Member tsungik's Avatar
    Join Date
    Aug 2004
    Location
    Philippines
    Posts
    194

    Re: [RESOLVED] Which is more efficient?

    Sorry for that Phill64.

    Thread already marked.
    Begin with the end in mind.

    My Profile

    while( !( succeed = try() ) );

  9. #9
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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
  •  



Click Here to Expand Forum to Full Width