• Welcome to Valhalla Legends Archive.
 

Booleans

Started by brew, May 06, 2007, 10:24 AM

Previous topic - Next topic
|

brew

What's more efficient, using "If blah Then" or "If blah = True Then"?
I heard that using "If blah = true then" is better, because the processor wouldn't have to convert it to a boolean before evaluating it. Is this true? or is If Blah Then better?
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Barabajagal

...convert it to a boolean? as long as you define it as boolean, it won't have to convert anything.
Option Explicit

Dim bolBlah as Boolean
  bolBlah = True
  If bolBlah Then MsgBox "This is efficient"

Dim bolBlah
  bolBlah = True
  if bolBlah Then MsgBox "This is just bad"

Option Explicit

Dim bolBlah as Boolean
  bolBlah = True
  If bolBlah = True Then MsgBox "This is a waste of 7 characters in your code"

l2k-Shadow

Quote from: brew on May 06, 2007, 10:24 AM
What's more efficient, using "If blah Then" or "If blah = True Then"?
I heard that using "If blah = true then" is better, because the processor wouldn't have to convert it to a boolean before evaluating it. Is this true? or is If Blah Then better?

I have to warn you though if you are using code such as:

If Not Function(x, y) Then


sometimes if the said function is located inside a non-VB dll, and it returns a bool, If Not will take a returned true value and yet think of it as false.

If Function(x, y) = False Then

works though.
Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

brew

#3
Well reality assuming it is defined as a boolean...
You know VB does alot of things we don't know about (for example those nice bstrs)
and i heard that no matter what type it is, the expression is converted to a boolean before being evaluated. so if it's being compared to a boolean, it wouldn't need to do this. Here's my interpretation:

Dim Blah As Boolean
Blah = True

If Blah Then
[converts number to a true or a false based on value] [compares]


If Blah = True Then
[compares boolean value to constant value]

You see? I see everywhere that using "If Variable Then" is better, but how? Am I wrong about how vb6 evaluates expressions? maybe?

Edit*
Quote
I have to warn you though if you are using code such as:
Code:
If Not Function(x, y) Then
Good example. In vb a boolean is either FALSE (0) or TRUE (-1). If the dll returns a value of (1) instead of (-1), Not 1 = -2, so it would mess up, because in general anything thats not 0 is true. (even more reason to compare it to a constant boolean value)
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Barabajagal

If Blah Then isn't an expression...

Try this: Make a form with two buttons and two lables. Buttons named cmdType1 and cmdType2. Labels named lblType1 and lblType2. Insert this code: Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Sub cmdType1_Click()
Dim Start As Long
Dim A As Boolean
Dim I As Long
    Start = GetTickCount
    For I = 0 To 999999
        A = True
        If A Then A = False
    Next I
    lblType1.Caption = GetTickCount - Start
End Sub

Private Sub cmdType2_Click()
Dim Start As Long
Dim A As Boolean
Dim I As Long
    Start = GetTickCount
    For I = 0 To 999999
        A = True
        If A = True Then A = False
    Next I
    lblType2.Caption = GetTickCount - Start
End Sub

On average, they're both similar, however, Type1 will be a little bit faster overall.

brew

Quote from: RεalityRipplε on May 06, 2007, 12:14 PM
If Blah Then isn't an expression...
If Blah Then isn't an expression, "Blah" itself is.
In C++ even doing "5;" is considered a complete expression.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

l2k-Shadow

#6
In VB, True and False are constants. True = -1, False = 0, so:
This code will not work

Dim a As Integer
     a = 1
     If a = True Then
          MsgBox "A = True"
     End If

HOWEVER:

Dim a As Integer
     a = 1
     If a Then
          MsgBox "A = True"
     End If

will work.

If a Then means the same as If Not a = False then.. whereas If a = True means if a = -1. They are two different expressions.
Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

Barabajagal

Expressions have to be evaluated. Blah isn't evaluated. Blah is a variable.

Quote from: l2k-Shadow on May 06, 2007, 12:35 PM
In VB, True and False are constants. True = -1, False = 0, so:
This code will not work

Dim a As Integer
     a = 1
     If a = True Then
          MsgBox "A = True"
     End If

HOWEVER:

Dim a As Integer
     a = 1
     If a Then
          MsgBox "A = True"
     End If

will work.

If a Then means the same as If Not a = False then.. whereas If a = True means if a = -1. They are two different expressions.
Which is why it's usually a good idea not to compare booleans to other variable types in vb.

l2k-Shadow

#8
Quote from: RεalityRipplε on May 06, 2007, 12:36 PM
Expressions have to be evaluated. Blah isn't evaluated. Blah is a variable.

If you're thinking in terms of booleans only, If a Then and If a = True Then are the SAME. Which is why the assembly of the code is the same for both cases.



Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

Barabajagal

I wrote two quick programs and used WinDif (comes with visual studio 6). They were different...

l2k-Shadow

edit again, i split my posts

Going more in depth, using this code (so the compiler is forced to evaluate the variable, instead of just going with constants like in the previous example):

Sub Main()
Dim a As Boolean
a = s
    If a Then
        MsgBox "A Then"
    End If
End Sub

Function s() As Boolean
    s = True
End Function


We see that If a Then compares it to another variable, evaluating the expression.

Sub Main()
Dim a As Boolean
a = s
    If a = True Then
        MsgBox "A True Then"
    End If
End Sub

Function s() As Boolean
    s = True
End Function


Here we see that If a = True Then compares a to 0xFFFF which as a signed integer is -1, evaluating the expression.
Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

Banana fanna fo fanna

Checking to see if a boolean is equal to True makes you look like a big n00b. This is important. People looking at code you write may be evaluating you for a job or something. Just stick with If SomeBoolean Then.

Besides, I feel like the efficiency gained from this is very negligible.

brew

Quote from: Banana fanna fo fanna on May 06, 2007, 01:14 PM
Checking to see if a boolean is equal to True makes you look like a big n00b. This is important. People looking at code you write may be evaluating you for a job or something. Just stick with If SomeBoolean Then.

Besides, I feel like the efficiency gained from this is very negligible.
It may seem noobish, but really we should be laughing at the people who think it is. In theory, comparing the value to a constant would be much more efficient then having it be evaluated first, like.... "If X then" Sure it sounds simpler, it may look easier, but is it really better? In order to check if "X" is true or false, it would have to be CBool()'d then re-evaluated before finally passing the If.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Barabajagal

I just proved it was better....

brew

Those kinds of tests aren't always accurate. For example, my good friend Ante tried to see what kind of operation would be fastest using exactly that kind of test, and apparently division is. (my ass it is)
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

|