Quicker If statements 
Author Message
 Quicker If statements

Do you suppose this would be quicker if I put the entire If on one line, or
is it better seperated like this?

If Button = 1 And X >= 0 And X <= AboutThirty5.Width And Y >= 0 And Y <=
AboutThirty5.Height Then
        If Not TimerControl.Enabled And QuestionBox.Visible And
AllowShortCut Then
            'Code here
    end if
end if

Thanks,
Bodi Klamph
Azure Dragon Software

begin 666 Bodi Klamph.vcf
M0D5'24XZ5D-!4D0-"E9%4E-)3TXZ,BXQ#0I..DML86UP:#M";V1I#0I&3CI"
M;V1I($ML86UP: T*14U!24P[4%)%1CM)3E1%4DY%5#I"2VQA;7!H0$%Z=7)E

`
end



Thu, 09 Nov 2000 03:00:00 GMT  
 Quicker If statements

Bodi

Quote:
> Do you suppose this would be quicker if I put the entire If on one line, or
> is it better seperated like this?

> If Button = 1 And X >= 0 And X <= AboutThirty5.Width And Y >= 0 And Y <=
> AboutThirty5.Height Then
>         If Not TimerControl.Enabled And QuestionBox.Visible And
> AllowShortCut Then
>             'Code here
>     end if
> end if

I approach these sort of situations using the logic "use the least processor
intensive and least likely to occur tests first".

So since your code isn't executed unless AllowShortCut has a value, it's
the simplest and easiest to evaluate, and since it appears to be the aspect
of test that most restrictive, I'd start with that.

VB has to do some work in order to evaluate an object's property, but since
TimerControl.Enabled appears to be up there with regard to precedence, I'd
go with that next. BTW testing False is faster that using the Not operator (VB
doesn't have to hit the operator's code in the runtime). As well, since Enabled
is the Timer's default property, you gain some access speed by not specifying
the property.

So, with that in mind, here's how I'd go about things:

  If AllowShortCut Then
    If TimerControl = False Then
      If Button = 1 Then

        If (X >= 0) And (X <= AboutThirty5.Width) And _
                  (Y >= 0) And (Y <= AboutThirty5.Height) Then
          'Code here
        End If

      End If   ' Button = 1
    End If   ' TimerControl = False
  End If   ' AllowShortCut

That's the gist of it. Obviously tweak this according to the actual level
of precedence.

--
Brad Martinez
http://members.aol.com/btmtz/vb

Visit the new CCRP site at:
http://www.mvps.org/ccrp

Please direct questions/replies to the newsgroup.



Thu, 09 Nov 2000 03:00:00 GMT  
 Quicker If statements

Why do you have brackets around these statements?

If (X >= 0) And (X <= AboutThirty5.Width) And  (Y >= 0) And (Y <=
AboutThirty5.Height) Then

Also, I assume by the way you worded it, that it's quicker to access an
control through its default property rather than referencing it?

Can you explain:
"BTW testing False is faster that using the Not operator (VB doesn't have to
hit the operator's code in the runtime). "?

Thanks again,
Bodi Klamph
Azure Dragon Software

Quote:

>Bodi

>> Do you suppose this would be quicker if I put the entire If on one line,
or
>> is it better seperated like this?

>> If Button = 1 And X >= 0 And X <= AboutThirty5.Width And Y >= 0 And Y <=
>> AboutThirty5.Height Then
>>         If Not TimerControl.Enabled And QuestionBox.Visible And
>> AllowShortCut Then
>>             'Code here
>>     end if
>> end if

>I approach these sort of situations using the logic "use the least
processor
>intensive and least likely to occur tests first".

>So since your code isn't executed unless AllowShortCut has a value, it's
>the simplest and easiest to evaluate, and since it appears to be the aspect
>of test that most restrictive, I'd start with that.

>VB has to do some work in order to evaluate an object's property, but since
>TimerControl.Enabled appears to be up there with regard to precedence, I'd
>go with that next. BTW testing False is faster that using the Not operator
(VB
>doesn't have to hit the operator's code in the runtime). As well, since
Enabled
>is the Timer's default property, you gain some access speed by not
specifying
>the property.

>So, with that in mind, here's how I'd go about things:

>  If AllowShortCut Then
>    If TimerControl = False Then
>      If Button = 1 Then

>        If (X >= 0) And (X <= AboutThirty5.Width) And _
>                  (Y >= 0) And (Y <= AboutThirty5.Height) Then
>          'Code here
>        End If

>      End If   ' Button = 1
>    End If   ' TimerControl = False
>  End If   ' AllowShortCut

>That's the gist of it. Obviously tweak this according to the actual level
>of precedence.

>--
>Brad Martinez
>http://members.aol.com/btmtz/vb

>Visit the new CCRP site at:
>http://www.mvps.org/ccrp

>Please direct questions/replies to the newsgroup.

begin 666 Bodi Klamph.vcf
M0D5'24XZ5D-!4D0-"E9%4E-)3TXZ,BXQ#0I..DML86UP:#M";V1I#0I&3CI"
M;V1I($ML86UP: T*14U!24P[4%)%1CM)3E1%4DY%5#I"2VQA;7!H0$%Z=7)E

`
end


Thu, 09 Nov 2000 03:00:00 GMT  
 Quicker If statements

Why do you have brackets around these statements?

If (X >= 0) And (X <= AboutThirty5.Width) And  (Y >= 0) And (Y <=
AboutThirty5.Height) Then

Also, I assume by the way you worded it, that it's quicker to access an
control through its default property rather than referencing it?

Can you explain:
"BTW testing False is faster that using the Not operator (VB doesn't have to
hit the operator's code in the runtime). "?

Thanks again,
Bodi Klamph
Azure Dragon Software

Quote:

>Bodi

>> Do you suppose this would be quicker if I put the entire If on one line,
or
>> is it better seperated like this?

>> If Button = 1 And X >= 0 And X <= AboutThirty5.Width And Y >= 0 And Y <=
>> AboutThirty5.Height Then
>>         If Not TimerControl.Enabled And QuestionBox.Visible And
>> AllowShortCut Then
>>             'Code here
>>     end if
>> end if

>I approach these sort of situations using the logic "use the least
processor
>intensive and least likely to occur tests first".

>So since your code isn't executed unless AllowShortCut has a value, it's
>the simplest and easiest to evaluate, and since it appears to be the aspect
>of test that most restrictive, I'd start with that.

>VB has to do some work in order to evaluate an object's property, but since
>TimerControl.Enabled appears to be up there with regard to precedence, I'd
>go with that next. BTW testing False is faster that using the Not operator
(VB
>doesn't have to hit the operator's code in the runtime). As well, since
Enabled
>is the Timer's default property, you gain some access speed by not
specifying
>the property.

>So, with that in mind, here's how I'd go about things:

>  If AllowShortCut Then
>    If TimerControl = False Then
>      If Button = 1 Then

>        If (X >= 0) And (X <= AboutThirty5.Width) And _
>                  (Y >= 0) And (Y <= AboutThirty5.Height) Then
>          'Code here
>        End If

>      End If   ' Button = 1
>    End If   ' TimerControl = False
>  End If   ' AllowShortCut

>That's the gist of it. Obviously tweak this according to the actual level
>of precedence.

>--
>Brad Martinez
>http://members.aol.com/btmtz/vb

>Visit the new CCRP site at:
>http://www.mvps.org/ccrp

>Please direct questions/replies to the newsgroup.

begin 666 Bodi Klamph.vcf
M0D5'24XZ5D-!4D0-"E9%4E-)3TXZ,BXQ#0I..DML86UP:#M";V1I#0I&3CI"
M;V1I($ML86UP: T*14U!24P[4%)%1CM)3E1%4DY%5#I"2VQA;7!H0$%Z=7)E

`
end


Thu, 09 Nov 2000 03:00:00 GMT  
 Quicker If statements

Why do you have brackets around these statements?

If (X >= 0) And (X <= AboutThirty5.Width) And  (Y >= 0) And (Y <=
AboutThirty5.Height) Then

Also, I assume by the way you worded it, that it's quicker to access an
control through its default property rather than referencing it?

Can you explain:
"BTW testing False is faster that using the Not operator (VB doesn't have to
hit the operator's code in the runtime). "?

Thanks again,
Bodi Klamph
Azure Dragon Software

Quote:

>Bodi

>> Do you suppose this would be quicker if I put the entire If on one line,
or
>> is it better seperated like this?

>> If Button = 1 And X >= 0 And X <= AboutThirty5.Width And Y >= 0 And Y <=
>> AboutThirty5.Height Then
>>         If Not TimerControl.Enabled And QuestionBox.Visible And
>> AllowShortCut Then
>>             'Code here
>>     end if
>> end if

>I approach these sort of situations using the logic "use the least
processor
>intensive and least likely to occur tests first".

>So since your code isn't executed unless AllowShortCut has a value, it's
>the simplest and easiest to evaluate, and since it appears to be the aspect
>of test that most restrictive, I'd start with that.

>VB has to do some work in order to evaluate an object's property, but since
>TimerControl.Enabled appears to be up there with regard to precedence, I'd
>go with that next. BTW testing False is faster that using the Not operator
(VB
>doesn't have to hit the operator's code in the runtime). As well, since
Enabled
>is the Timer's default property, you gain some access speed by not
specifying
>the property.

>So, with that in mind, here's how I'd go about things:

>  If AllowShortCut Then
>    If TimerControl = False Then
>      If Button = 1 Then

>        If (X >= 0) And (X <= AboutThirty5.Width) And _
>                  (Y >= 0) And (Y <= AboutThirty5.Height) Then
>          'Code here
>        End If

>      End If   ' Button = 1
>    End If   ' TimerControl = False
>  End If   ' AllowShortCut

>That's the gist of it. Obviously tweak this according to the actual level
>of precedence.

>--
>Brad Martinez
>http://members.aol.com/btmtz/vb

>Visit the new CCRP site at:
>http://www.mvps.org/ccrp

>Please direct questions/replies to the newsgroup.

begin 666 Bodi Klamph.vcf
M0D5'24XZ5D-!4D0-"E9%4E-)3TXZ,BXQ#0I..DML86UP:#M";V1I#0I&3CI"
M;V1I($ML86UP: T*14U!24P[4%)%1CM)3E1%4DY%5#I"2VQA;7!H0$%Z=7)E

`
end


Thu, 09 Nov 2000 03:00:00 GMT  
 Quicker If statements

It is always best to split the if into as many operations as possible,
because VB does not use lazy evaluation, and will thus evaluate the whole
line even if Button does not equal 1.

Matthew



Quote:
> Do you suppose this would be quicker if I put the entire If on one line,
or
> is it better seperated like this?

> If Button = 1 And X >= 0 And X <= AboutThirty5.Width And Y >= 0 And Y <=
> AboutThirty5.Height Then
>         If Not TimerControl.Enabled And QuestionBox.Visible And
> AllowShortCut Then
>             'Code here
>     end if
> end if

> Thanks,
> Bodi Klamph
> Azure Dragon Software



Thu, 09 Nov 2000 03:00:00 GMT  
 Quicker If statements

Bodi,

Brackets make no difference in this example - they just make it easier to
read.

I would use them in the same way.

Matthew



Quote:
> Why do you have brackets around these statements?

> If (X >= 0) And (X <= AboutThirty5.Width) And  (Y >= 0) And (Y <=
> AboutThirty5.Height) Then

> Also, I assume by the way you worded it, that it's quicker to access an
> control through its default property rather than referencing it?

> Can you explain:
> "BTW testing False is faster that using the Not operator (VB doesn't have
to
> hit the operator's code in the runtime). "?

> Thanks again,
> Bodi Klamph
> Azure Dragon Software



Thu, 09 Nov 2000 03:00:00 GMT  
 Quicker If statements

Andrew,

Quote:
>>"BTW testing False is faster that using the Not operator (VB doesn't have
to
>>hit the operator's code in the runtime). "?

While, the above statement is true, testing false and using Not will not
always produce the same results (if you are NOT dealing with booleans).
They will in your particular example though.

Using the following code:

    Dim intCounter As Integer

    For intCounter = -1 To 1

        If Not intCounter Then                  '0, 1
            MsgBox "Not " & intCounter
        End If

        If intCounter Then
            MsgBox "Is " & intCounter             '-1, 1
        End If

        If intCounter = True Then
            MsgBox "True  " & intCounter          '-1
        End If

        If intCounter = False Then                      '0
            MsgBox "False " & intCounter
        End If
    Next

If intCounter is True or False (-1 or 0) the functionality is as normal.
But if intcounter = 1, then intcounter = False is False and intCounter =
True is false, but intCounter is True (and also Not intcounter is True!).

While this is a little off-topic, it is important to note.

Matthew



Sun, 12 Nov 2000 03:00:00 GMT  
 Quicker If statements

: Why do you have brackets around these statements?

: If (X >= 0) And (X <= AboutThirty5.Width) And  (Y >= 0) And (Y <=
: AboutThirty5.Height) Then

: Also, I assume by the way you worded it, that it's quicker to access an
: control through its default property rather than referencing it?

: Can you explain:
: "BTW testing False is faster that using the Not operator (VB doesn't have to
: hit the operator's code in the runtime). "?

The code:
          If Not TimerControl.Enabled
is in fact 2 operations:
          TestResult = (TimerControl.Enabled = True)?
          TestResult = Not TestResult
so by re-writing the original IF statement to :
          If TimerControl.Enabled = False
you have only one operation that has exactly the same result.
Needless to say this small change will execute your code faster.

: Thanks again,
: Bodi Klamph
: Azure Dragon Software

: begin 666 Bodi Klamph.vcf
: M0D5'24XZ5D-!4D0-"E9%4E-)3TXZ,BXQ#0I..DML86UP:#M";V1I#0I&3CI"
: M;V1I($ML86UP: T*14U!24P[4%)%1CM)3E1%4DY%5#I"2VQA;7!H0$%Z=7)E

: `
: end

- Drew
--
 ***********************************************************************

 * Progress:-  Mans ability to make bigger mistakes sooner and faster. *
 * Optimization:- The pursute to produce errors more quickly and with  *
 *                less waste.                                          *
 ***********************************************************************



Mon, 13 Nov 2000 03:00:00 GMT  
 Quicker If statements

If you're really pinching for speed, separate each condition in it's own  IF
statement because VB does not short ciruit evaluate.  If you string 3
together, it has to test all 3 even though the first one fails.

Quote:

>Andrew,

>>>"BTW testing False is faster that using the Not operator (VB doesn't have
>to
>>>hit the operator's code in the runtime). "?

>While, the above statement is true, testing false and using Not will not
>always produce the same results (if you are NOT dealing with booleans).
>They will in your particular example though.

>Using the following code:

>    Dim intCounter As Integer

>    For intCounter = -1 To 1

>        If Not intCounter Then '0, 1
>            MsgBox "Not " & intCounter
>        End If

>        If intCounter Then
>            MsgBox "Is " & intCounter '-1, 1
>        End If

>        If intCounter = True Then
>            MsgBox "True  " & intCounter '-1
>        End If

>        If intCounter = False Then '0
>            MsgBox "False " & intCounter
>        End If
>    Next

>If intCounter is True or False (-1 or 0) the functionality is as normal.
>But if intcounter = 1, then intcounter = False is False and intCounter =
>True is false, but intCounter is True (and also Not intcounter is True!).

>While this is a little off-topic, it is important to note.

>Matthew



Mon, 13 Nov 2000 03:00:00 GMT  
 Quicker If statements

Hey, a posting in VB.syntax about syntax, not implementation issues.

My first answer is that usually when acting on a user event the amount of
time spent on conditional statements is irrelevant, i.e. .5 sec or .45 sec
is unnoticeable to the user.

Its code deep in loops that gets the most from optimisation, there is always
a trade between size and speed, anyone for more bloatware ?

If the snippet is compiled it should be optimised to the same representation
conditional jumps, you might be able to speed it up by bringing some
information that cannot be synthesised.

i.e. placing common ages earlier in a conditional statement and not in a
chronological sequence.

Quote:

>Do you suppose this would be quicker if I put the entire If on one line, or
>is it better separated like this?

But your question brings something to light that I was going to post on its
own.

You had 2 complex conditions with embedded action and no reporting.

Rewriting your example

Quote:
>If Button = 1 _

    And X >= 0 _
    And X <= AboutThirty5.Width _
    And Y >= 0 _
    And Y <= AboutThirty5.Height Then

Quote:
>        If Not TimerControl.Enabled And QuestionBox.Visible And
>AllowShortCut Then
>            'Code here

    Else
        Error "Either Question Not Rejected or AllowShortCut Selected, OK ?"
Quote:
>    end if

Else
    Error "Something Bad Happened, Re-install Yes/No"

Quote:
>end if

Note the less than meaningful messages, remind you of error messages you've
seen but left you saying "Wot".

Taking your example, sorry about the length but I felt I had to replace
every condition in your snippet.
It also shows the extension in code by breaking the big condition into lots
of small ones.
Anyone for $$$ for KLOCs ?

'Overlong Code Example
If Button <> 1 Then
    Error "Ejection Moulding Retaining Cap Needs To Be Enabled"
    Exit Sub
ElseIf X < 0 Then
    Error "Di-Lithiam Crystals De Energised"
    Exit Sub
ElseIf X > AboutThirty5.Width Then
    Error "Di-Lithiam Crystals Critical"
    Exit Sub
ElseIf Y < 0 Then
    Error "Heisenberg Compensators De Energised"
    Exit Sub
ElseIf Y > AboutThirty5.Height Then
    Error "Heisenberg Compensators Critical"
    Exit Sub
ElseIf Not TimerControl.Enabled Then
    Error "Tardis Chronos Phase Enducer Disabled"
    Exit Sub
ElseIf Not QuestionBox.Visible Then
    Error "Please Tick Question Required"
    Exit Sub
ElseIf Not AllowShortCut Then
    Error "Ensure That AllowShortCut Is Selected"
    Exit Sub
End If

'Code here, note not within a final bare Else clause.

Quote:

>Thanks,
>Bodi Klamph
>Azure Dragon Software

Now you've got this far, which is the better style, 'easy coding' vs. 'easy
reporting'.

Other points, conditions at the end of a long sequence might not be
evaluated due to short circuit evaluation and represent dead code.

TTFN John



Tue, 14 Nov 2000 03:00:00 GMT  
 Quicker If statements

John,

Quote:
> Other points, conditions at the end of a long sequence might not be
> evaluated due to short circuit evaluation and represent dead code.

> TTFN John

Are you saying VB uses short circuit (ie. lazy) evaluation? I don't think
so...

Matthew



Thu, 16 Nov 2000 03:00:00 GMT  
 Quicker If statements

Oh Oh

|John,
|
|> Other points, conditions at the end of a long sequence might not be
|> evaluated due to short circuit evaluation and represent dead code.
|>
|> TTFN John
|
|Are you saying VB uses short circuit (ie. lazy) evaluation? I don't think
|so...

I found out before reading your post just how wrong I was, don't hold it
against me as my code used short circuit evaluation (evaluates at least
once) VB didn't need to be.

Lazy evaluation allows zero evaluation ?

P.S. I prefer the 'AND THEN', 'OR ELSE' optional constructs to give us poor
programmers the ability to choose how we will hang themselves.

TTFN agin John

|Matthew



Tue, 05 Dec 2000 03:00:00 GMT  
 Quicker If statements

John,

Just for everyone's benefit!

Lazy evaluation is used by C and some languages. VB doesn't have it.

Example:

If function1 And function2 then
        do code
endif

A language that supports lazy evaluation will only call function2 if
function1 has returned something which evaluates to True (since if
function1 is False then Function1 and Function 2 WILL DEFINITELY BE FALSE).
VB will however, not do this (it will ALWAYS call function2) - this has
advantages and disadvantages.

Matthew



Quote:
> Oh Oh


> |John,
> |
> |> Other points, conditions at the end of a long sequence might not be
> |> evaluated due to short circuit evaluation and represent dead code.
> |>
> |> TTFN John
> |
> |Are you saying VB uses short circuit (ie. lazy) evaluation? I don't
think
> |so...

> I found out before reading your post just how wrong I was, don't hold it
> against me as my code used short circuit evaluation (evaluates at least
> once) VB didn't need to be.

> Lazy evaluation allows zero evaluation ?



Sat, 09 Dec 2000 03:00:00 GMT  
 Quicker If statements

Although VB doesn't directly support lazy evaluation, it is easy to
accomplish the same thing.

Instead of "If Function1 and Function2", use
'------
If Function1 then
If Function2 then
'Put your code here
End If
End If
'--------

Russell Davis


Quote:
>John,

>Just for everyone's benefit!

>Lazy evaluation is used by C and some languages. VB doesn't have it.

>Example:

>If function1 And function2 then
> do code
>endif

>A language that supports lazy evaluation will only call function2 if
>function1 has returned something which evaluates to True (since if
>function1 is False then Function1 and Function 2 WILL DEFINITELY BE
FALSE).
>VB will however, not do this (it will ALWAYS call function2) - this has
>advantages and disadvantages.

>Matthew



>> Oh Oh




- Show quoted text -

Quote:
>> |John,
>> |
>> |> Other points, conditions at the end of a long sequence might not
be
>> |> evaluated due to short circuit evaluation and represent dead code.
>> |>
>> |> TTFN John
>> |
>> |Are you saying VB uses short circuit (ie. lazy) evaluation? I don't
>think
>> |so...

>> I found out before reading your post just how wrong I was, don't hold
it
>> against me as my code used short circuit evaluation (evaluates at
least
>> once) VB didn't need to be.

>> Lazy evaluation allows zero evaluation ?



Sun, 10 Dec 2000 03:00:00 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Where can I get Quick Quick-basic

2. API, WM_SETEXT, quick question, need quick answer please

3. Expected:line number or label or statement or end of statement

4. running sql code record by record instead of statement by statement

5. With...End With statement bombing out in For...Next statement

6. Very simple - How to execute sub statements before the end statement

7. Combine a Loop Statement and If Statement?

8. Update statement creates Error, when statement is fine

9. Two quick questions (Yeah, like anything's quick in Access!)

10. How do I make it go quicker? Quicker yet

11. Quick Question - acOpenForm

12. Auto Quick Info

 

 
Powered by phpBB® Forum Software