Which is better?
If Not A = B Then...
-OR-
If A <> B Then...
They are logically identical, right? But, which one uses less memory/cpu cycles/quantum foam/etc?
Which is better?
If Not A = B Then...
-OR-
If A <> B Then...
They are logically identical, right? But, which one uses less memory/cpu cycles/quantum foam/etc?
Use Timer and see if you can tell the difference. I would be surprised if there is any actual difference at all, though.
Please Login or Register to view this content.
I bet you a nickle that if there is a difference, it might be measurable in nano-seconds. I'm OCD enough, that I'm talking CPU clock cycles.
So was there a difference with Timer?
The code this question came up in is way more complex than this simple question. :P
I enjoy deep logical conundrums. Things like, "If Not ( A Or B )" is logically identical to "If Not A And Not B". Readability would dictate which way I write it, but I was curious as to which was more CPU-friendly.
I suppose the obvious thing would be to use the Timer on your actual code, report back and put us out of our misery.
Unfortunately, I believe that anything that I could try with timer would only give me a subjective answer that couldn't answer the deeper question.
I started thinking about this by reading here: http://www.ozgrid.com/VBA/SpeedingUpVBACode.htm
Perhaps I can ask there.
Not is a logical operation, whereas <> can also apply to strings, numbers etc. If A and B are Boolean, then it will be quicker to use Not as that is a simple inversion of bits. Using IF is likely to take many more CPU cycles, however, as that is much more processor-intensive.
Hope this helps.
Pete
'fraid it's not very clear to me just what you consider a "subjective answer" in this context
nor what actually is the "deeper question"
perhaps could you be more specific? do I assume your question is practical (say concerning computer capacity) or just of theoretical interest?
regarding your opening post and the comments about timer in the thread, a simple coded example may give some clues
change the red line to your "logically identical" version and note the difference
there is a difference with my computer, 1.93 secs with the Not = and 1.19 secs with the <> (which is 62% time difference)Please Login or Register to view this content.
Last edited by kalak; 11-26-2014 at 03:32 PM.
They aren't logically equivalent..
Please Login or Register to view this content.
Last edited by Norie; 11-27-2014 at 06:25 AM.
If posting code please use code tags, see here.
My dad bought me a logic game called "wyff-n-proof" when I was 12. That taught me logical operators.
Regardless of what A and B are (bool, str, int, etc); if they are equal or not equal, logic will answer the if/then.
If A(of any type) = B(of the same type) Then (whatever).
If A <> B Then (whatever).
If Not(A = B) Then (whatever).
The last two are logically identical.
Rules get really odd, when you are doing boolean compares:
If Not A Or Not B Then... does one thing.
If Not (A And B) Then ... does the same thing.
You can prove this with a Karnaugh Map:
http://en.wikipedia.org/wiki/Karnaugh_map
( cross post: http://www.ozgrid.com/forum/newreply...reply&t=192068 )
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks