Short-circuit evaluation with Select Case

Short-circuit evaluation with Select Case

Short-circuit evaluation is an optimization technique automatically adopted by most modern compilers, including all flavors of C++, Borland Delphi and many others. Unfortunately, the Visual Basic compiler is not in this group. This optimization cuts down the time needed to evaluate a boolean expression, such as:

If x > 0 And y ^ 2 < x Then Call DoItIf x = 0 Or Log(y) = z Then Call DoIt

In the first expression, if x is less than or equal to zero, the "Then" block is skipped over without evaluating the "y^2" sub-expression, because even if it were True the combined ANDed result would be False anyway. Similarly, in the second expression "Log(y)=x" is not evaluated if the X variable happens to be zero.

Even if the VB compiler doesn't support short-circuit evaluation, you can manually enforce it by using nested If statements, as in:

If x > 0 Then    If y ^ 2 < x Then Call DoItEnd If...If x = 0 Then    Call DoItElseIf Log(y) = z Then     Call DoItEnd If

However, things gets quickly complex when there are more than just two sub-expressions tied with a boolean operator. Here's is an unorthodox way to use Select Case to enforce short-circuit evaluation:

' If x > 0 And y <= 0 And z = 0 Then DoItSelect Case False    Case x > 0, y <= 0, z = 0     Case Else        Call DoItEnd Select...' If x > 0 Or y <= 0 Or z = 0 Then DoItSelect Case True    Case x > 0, y <= 0, z = 0         Call DoItEnd Select

Surprisingly, the Select Case conditional block does short-circuit evaluation! The above Select Case blocks are about three times faster than the equivalent If statements when the first sub-expression is enough to decide if the compound boolean expression will be False (And) or True (Or). The Select Case approach is still 30% faster when it is necessary to evaluate two sub-expressions. On the other hand it is slightly slower, by about 20%, if all three sub-expressions must be evaluated. However, relative timing might differ - and make the Select Case technique even more advantageous, if the sub-expressions after the first one are more complex, include math operations (especially and transcendental functions such as Sin or Log), string function, etc.

For the best results of this method, stick to the following rules:

  • Place first the simplest sub-expressions, and keep the most complex ones at the end of the list
  • if you have sub-expressions of similar complexity, place first those that are more likely to return False (in case of ANDed expressions) or True (in case of ORed ones)
At any rate, I strongly suggest you to resort to this trick only if you have more than two sub-expressions and you're within a time-critical loop, and - above all - to add a remark that clearly shows what you're doing, otherwise it would be very difficult to understand your own code after a few weeks, or even days.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes