checking conditions?

Iketh

Active member
Joined
Nov 23, 2009
Messages
40
Programming Experience
Beginner
Ok how about this...

FIRST = arguments checked against each other
SECOND = arguments checked against each other but are irregardless of the FIRST arguments.

VB.NET:
If (FIRST and (FIRST or (FIRST and FIRST))) OR (SECOND or (SECOND or SECOND or (SECOND and (SECOND or SECOND)))) then .....

As the program processes this line over a million times during the execution, only FIRST or SECOND section is needed during an execution, never both. How do I make the program use only the section that's needed for the current execution instead of checking every argument every time? Is there a way to turn a string into an argument list for an IF...THEN statement? As in:

VB.NET:
String1 = "FIRST and (FIRST or (FIRST and FIRST))"
String2 = "SECOND or (SECOND or SECOND or (SECOND and (SECOND or SECOND)))"

If String1 Then ...

Is something like this possible?
 
Last edited:
Perhaps you mean OrElse Operator ?

If not, you can nest If-Then blocks:
VB.NET:
If checkFirst Then
  If First blah blah

ElseIf checkSecond
  If Second blah blah
Or perhaps you didn't mean any of this... :confused:
 
First up, never turn anything into a String unless you specifically want a String. That's always going to slow things down.

Secondly, if all your FIRSTs and SECONDs are Boolean expressions then why would you think to create a String anyway? Combining any number of Booleans using logic results in another Boolean. If your original Boolean expressions don't change then their combination won't change, so just combine them once and assign the result to a Boolean variable, then use that over and over. If the original values do change then you've got no choice bu tto evaluate them all every time.
 
The string would be set before the execution began. But i guess that's not possible anyhow so...

I read a little about partial classes and it was a little over my head at this point but it seemed to be the solution I am looking for...

I need to illustrate it a bit more.

VB.NET:
If [bunch of arguments of which only half or 1/4 are needed during an execution] then
elseif [ditto] then
elseif [ditto] then
elseif [ditto] then
elseif [ditto] then
elseif [ditto] then
elseif [ditto] then
elseif [ditto] then
elseif [ditto] then
elseif [ditto] then
elseif [ditto] then
elseif [ditto] then
elseif [ditto] then
elseif [ditto] then
end if

So i cannot nest or the elseif block gets prematurely terminated. There's about 20-30 elseif statements total in the block.

To nest, I would have to do this...

VB.NET:
[set a boolean to true]
If ... Then
   [FALSE the boolean showing this code was accessed]
End If
If [read the boolean]
   If ... Then
      [FALSE the boolean showing this code was accessed]
   End If
End If
If [read the boolean]
   If ... Then
      [FALSE the boolean showing this code was accessed]
   End If
End If
If [read the boolean]
   If ... Then
      [FALSE the boolean showing this code was accessed]
   End If
End If

etc...

Then i could nest all I want to, but this is how I originally laid out the code and when I cleaned it up with one large if...elseif block, it proved significantly faster. Im just trying to take it a step further.

The arguments are not all boolean. Half are small to large equations with <>= operands.
 
If the original values do change then you've got no choice bu tto evaluate them all every time.

alright thanks, Ill just leave it like it is. You have me dying to migrate to C++. Writing efficient code is a passion... is that weird?
 
You could use a function that returns a Boolean to keep all of boolean checks in one place.

VB.NET:
Private Function IsGreater(ByVal amt As integer) As Boolean
   If amt > Me.BigNumber Then
       Return True
   Else 
       Return False
   End If
End Function

This is also good practice when you call the same code over and over again.
Not sure if it will work in your case, just an idea.
 
Functions or any other subroutine calls in the speed-critical part of the program destroys its speed. I cannot use them.
 
Functions or any other subroutine calls in the speed-critical part of the program destroys its speed. I cannot use them.
I will say again, if speed is so critical that you can't even make a function call then you shouldn't be using VB.NET in the first place.
 
If there's only a small part of the app that requires extreme performance then you may just be able to break that part out into a DLL and write that in another language. As I said, C# supports pointers natively so just that may be enough to provide boost you need. In certain circumstances, using pointers can increase efficiency by orders of magnitude. C# will be far easier for a VB.NET developer to come to terms with than C++ too.

If just using pointers is not enough then you may need to resort to unmanaged C++. Using it just for a few calculations is far easier than creating entire applications though.
 
Good tips jmcil, thanks. When I release the app, Ill point to the source for the engine in the application and you can give me your opinion on jumping straight into C++ or C#. Also, I see that VB code can convert directly into C# and vice versa?

JohnH, does the OrElse operator split up how much of the IF statement is processed at once? As in it checks everything before it, and if false, it checks everything after it? Sorry I haven't googled this one yet.
 
JohnH, does the OrElse operator split up how much of the IF statement is processed at once? As in it checks everything before it, and if false, it checks everything after it? Sorry I haven't googled this one yet.
Open the help topic I linked to again and read the remark section, it explains exactly that.
 
Oh wow thanks John, that's exactly what I was looking for. I really didn't believe something like that existed, I'm glad I asked.
It's just too bad that in half the situations, it won't improve performance because the code before the OrElse will always be read.
 
If you only need to evaluate one of the expressions you have to determine which first and simply use a If statement to branch to either one. The short-circuit operators is for those cases where you need to evaluate two (or more) expressions and where you only need to evaluate second expression depending on the outcome of the first, in two of out three cases they have same functionality and purpose as their And/Or counterparts.
 
Back
Top