What is faster? "Not =" or <> ? (Super Easy ?)

which would execute faster, and what is better codeing convention?

if A <> 1 then mysub

or

if Not A = 1 then mysub

thanks
JaysorAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
amebaConnect With a Mentor Commented:
>Also,  
>   If not X
>is faster then
>   If X = 0
>and "Not" statement works on all numeric types.

Sorry, that is not correct. This is correct:
   If not CBool(X)
is faster then
   If X = 0

See also:
http://www.vb2themax.com/Item.asp?PageID=TipBank&ID=129
http://www.vb2themax.com/Item.asp?PageID=TipBank&ID=119 (mark mentioned this)
You can find many tips in TipBank section.
0
 
mark2150Commented:
Depends on the nature of A. I'd generally prefer the first form if A is an integer. The second form you're showing should be:

IF NOT (A = 1) THEN CALL MySub

The paren's help the compiler know you're after a logical result.

Overall I think the first form would be faster.

However, there are some cases when you can get around having an IF altogether. Suppose you're showing/hiding a control on the basis of a control value. Lets say we only want to enable a button if some value is non-zero. You might code:

IF SomeValue = 0 THEN
  MyButton.Enabled = True
ELSE
  MyButton.Enabled = False
END IF

However you can simplify this to:

MyButton.Enabled = (SomeValue = 0)

The compiler will evaluate the (SomeValue = 0) clause as either TRUE or FALSE and will therefore be able to use that to change the boolean state of the control's .Enabled property. This is four less lines of code and assures that you can't accidentially forget to code the "bottom half" of the conditional.

M
0
 
JaysorAuthor Commented:
Adjusted points to 15
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JaysorAuthor Commented:
thanks for the () tip. also what do you think about boolean valuse? ie:

If Not myBoolean Then MySub or

If myBoolean = False then MySub

0
 
amebaCommented:
The fastest is:
   If Not A - 1 Then mysub
0
 
mark2150Commented:
Jaysor,

I prefer the first form as long as myBoolean is a "real" boolean. I've got some legacy code where the original programmer used integers with 0 for false and 1 for true.

I do also prefer the CALL MySub rather than the straight MySub. This also helps make it clear that it's a sub an not a function, but that is more a matter of taste than anything else.

Ameba, didn't you mean:

If not A = 1 ...

instead of

if not A - 1 ...

?

M
0
 
amebaCommented:
>Ameba, didn't you mean:
>If not A = 1 ...
No. Comparison is slower then mathematical operation.

Also,  
   If not X
is faster then
   If X = 0
and "Not" statement works on all numeric types.
0
 
amebaCommented:
Also, for strings:
' veeery slow:
  If X = "" Then

' fast
  If len(X) = 0 Then

' faster
  If len(X) Then
0
 
paul_tsekovCommented:
Hi VBfan,
1.
When using Boolean values,
use Not()
2.
When using integer and numberic values
use <>
  The <> works better and looks better if you have some numberic value.
  The Not looks better but doesn't work better if you have a boolean value.

When using boolean values:
dim b as boolean
    if Not b then
        ...
    end if
    if b then
        ...
    end if

if preffered than :

It look better than
  if (b<>True) then
        ...
  end if
  if  (b<>False) then
       ...
  end if






if you have the numberic value:
     if a<>10 then
         ...
     end if
Is preffered than:
     if Not(a=10) then
         ...
     end if
and works faster.

         
   
              Pavel Tsekov
       email : paul_tsekov@yahoo.com
0
 
JaysorAuthor Commented:
Im rejecting it because "ameba's" answered first and was more consise with links! How could i ask for more!

0
 
JaysorAuthor Commented:
thanks for the links too!
0
 
amebaCommented:
Thanks
0
All Courses

From novice to tech pro — start learning today.