Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not x = 0 or x<>0

Status
Not open for further replies.

enui71

Programmer
Feb 21, 2001
12
US
Is there a performance difference? Why do I never see the Not in examples or documentation?

Just curious?
 
I think just because the first one looks awful and is harder to read.
I never understand why people insist on writing
if a > 0 then
b = true
else
b=false
endif

instead of
b= ( a > 4 )

Probably because I started off writing in machine code and then assembler! If you wasted a byte you got soundly told off.
 
but in this specific instance, use :

If (X) then
... 'Your code
End If

This just checks tha "X' is NOT = 0,

So WHY would you do the double evaluation?

1: X = 0
2: Not (expr) 1

The only difference might be if you don't use Option Explicit or some weird Dim of X, so that <> = is not the same as zero (Dim X as a string or variant?) I think I'm really going way to far with this? Don't You?


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Yeah MichealRed you did go to far. Wasn't specific, x = 0 was just used to show syntax.

Petermeachem, I agree with b = a > 4. Use that style also. Also have a history in Assembler, so have that same &quot;don't waste a byte idea&quot;

My question was basically is (Not =) slower than (<>)? Or is there another reason it seldom used?

I don't find it harder to read. I can see how if the &quot;not&quot; is brushed over when reading, it could be trouble.
 
With an Assembler background, you must know Not (A = B) is two operations, while A <> B is just one. A good compiler can restructure the expression to reduce it in the object code, but who knows how well VBA optimizes?

I ran a benchmark to compare the two. Even though I used a For loop of 20,000 iterations and did the expression evaluation 50 times in the loop, giving 1 million calculations, I couldn't get either of them to register taking any time at all. Maybe VBA has some pretty global optimization after all? Did it throw away 49 of my statements within the loop? Did it even eliminate the loop entirely, since I didn't use the result value anywhere? I wish Microsoft would actually document this stuff sometimes. Rick Sprague
 
Well, intrigued by Ricks results, I did another 'benchmark'. Ignoring the nicities of loops within loops, I just ran the two with 1 mill executions. Rick 'appeared' to be correct. Being partly Missourian (&quot;SHOW ME&quot;), I then Upped the itteraztion count to 10 M. MS. VB gave a result - And it was different! I got 4 Seconds for the Not (Expr) and 5 Sec for the <>(expr). Encouraged by the apparent improvement in my mastery of the subject of benchmaking, I Lept forth into the fray and bumped the loop counter to 10M! Confidently awaiting my results, and sure that they would continue the trend of clear seperation, I went to get more caffiene.

Oh NO! Now, w/ 10M itterations, I get 44 Sseconde for BOTH approaches. Thus reverting to the original thesis. There IS no Difference, but confirming that Ms. VB doesn't &quot;magically&quot; compile my loops out of existance.


MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Didn't remember that A <> B was one operation. Glad that I sparked a some thought. Thanks for the info.

I also did a little benchmarking and came up with similar results. I don't commonly trust benchmarks on single opertions. I even received negitive times while benching this. And the code was correct. I ran it 5 times and recieved 2 negative results, back to back. So either I discovered time travel or something wierd happened. I vote for the later.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top