Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Are there any benefits or drawbacks in doing this :

VB
If colmod1 = new_colmod1 Then
        Else
            If colmod1 > new_colmod1 Then colmod1 -= 0.001
            If colmod1 < new_colmod1 Then colmod1 += 0.001
        End If


Rather than doing this? :

VB
If colmod1 <> new_colmod1 Then
            If colmod1 > new_colmod1 Then colmod1 -= 0.001
            If colmod1 < new_colmod1 Then colmod1 += 0.001
        End If


Does it compile down to the same code? should I avoid it because?


What I have tried:

N/A..Just for background info and what peoples opinions are.
Posted
Updated 26-Sep-17 7:46am
Comments
5everin 22-Sep-17 15:28pm    
Ps.
In this particular example the code performs a lot of these tests which will mostly find the values match. Its looking for exceptions in a large dataset.

The first one is ugly, and harder to read: there should be no performance difference once optimisation is completed.
 
Share this answer
 
Comments
5everin 22-Sep-17 16:12pm    
Thanks, best practice wasn't really what I was after though I understand why you added it in the answer.
They are (visual) basically the same. You might choose the first form just to upset Griff.
 
Share this answer
 
Comments
OriginalGriff 22-Sep-17 16:14pm    
:laugh: +5
Better question. Why are you testing to see if the value is "greater than" and then in the next statement testing to see if the value is "less than"?

If the value is not equal, you only need one other if statement to get the two outcomes.
VB
If colmod1 <> new_colmod1 Then
    If colmod1 > new_colmod1 Then 
        colmod1 -= 0.001
    Else
        colmod1 += 0.001
    End If
End If
 
Share this answer
 
Comments
5everin 22-Sep-17 16:29pm    
less typing for the example :)
Dave Kreskowiak 22-Sep-17 16:56pm    
Also less readable and less performant as an if statement is not a fast operation.
Richard Deeming 26-Sep-17 14:10pm    
The code within the If block modifies one of the variables being tested, so it's theoretically possible for the code to execute both blocks. :)

Eg:
colmod1 = 42
new_colmod1 = 41.9991
Dave Kreskowiak 26-Sep-17 17:29pm    
True. I saw that but it didn't even occur to me.
The overlying equal/notEqual, greaterThan, lessThan pattern is what stuck in my mind.
I did a little testing using the stopwatch and 1,000,000,000 iterations of the condition repeated 10 times for each version.

And to answer my own question:

The "nice" soloution is marginally faster than the "ugly" one when the condition always fails. The difference is only only about 1% or less but it seems to be fairly consistent over a few runs.

I guess the Else is more expensive to process.

The "ugly" form seems to be a little faster when the condition is always met by a slighty larger margin maybe 1-2%.

However once the compiler optimisations are turned on in VS. The difference disapears. So indeed there appears to be no reason to use it other than the annoyance factor :)
 
Share this answer
 
v5
Comments
Dave Kreskowiak 26-Sep-17 14:12pm    
Yeah, you were already told that the differences would disappear once you went with a build with optimizations turned on, a.k.a. "Release" build.

Testing a "Debug" build is a bit pointless.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900