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.
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
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 ("SHOW ME", 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 "magically" 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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.