+ Reply to Thread
Results 1 to 11 of 11

Simple question - (If Not A = B Then... ) vs ( If A <> B Then...)

  1. #1
    Forum Contributor Rhudi's Avatar
    Join Date
    03-08-2013
    Location
    South Carolina, US
    MS-Off Ver
    Professional Plus 2016 aka Office 365
    Posts
    199

    Simple question - (If Not A = B Then... ) vs ( If A <> B Then...)

    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?

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Simple question - (If Not A = B Then... ) vs ( If A <> B Then...)

    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.

  3. #3
    Forum Contributor Rhudi's Avatar
    Join Date
    03-08-2013
    Location
    South Carolina, US
    MS-Off Ver
    Professional Plus 2016 aka Office 365
    Posts
    199

    Re: Simple question - (If Not A = B Then... ) vs ( If A <> B Then...)

    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.

  4. #4
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Simple question - (If Not A = B Then... ) vs ( If A <> B Then...)

    So was there a difference with Timer?

  5. #5
    Forum Contributor Rhudi's Avatar
    Join Date
    03-08-2013
    Location
    South Carolina, US
    MS-Off Ver
    Professional Plus 2016 aka Office 365
    Posts
    199

    Re: Simple question - (If Not A = B Then... ) vs ( If A <> B Then...)

    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.

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Simple question - (If Not A = B Then... ) vs ( If A <> B Then...)

    I suppose the obvious thing would be to use the Timer on your actual code, report back and put us out of our misery.

  7. #7
    Forum Contributor Rhudi's Avatar
    Join Date
    03-08-2013
    Location
    South Carolina, US
    MS-Off Ver
    Professional Plus 2016 aka Office 365
    Posts
    199

    Re: Simple question - (If Not A = B Then... ) vs ( If A <> B Then...)

    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.

  8. #8
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,812

    Re: Simple question - (If Not A = B Then... ) vs ( If A <> B Then...)

    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

  9. #9
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Simple question - (If Not A = B Then... ) vs ( If A <> B Then...)

    '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.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Simple question - (If Not A = B Then... ) vs ( If A <> B Then...)

    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.

  11. #11
    Forum Contributor Rhudi's Avatar
    Join Date
    03-08-2013
    Location
    South Carolina, US
    MS-Off Ver
    Professional Plus 2016 aka Office 365
    Posts
    199

    Re: Simple question - (If Not A = B Then... ) vs ( If A <> B Then...)

    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 )

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. A simple question requiring a simple answer
    By Pedros in forum Excel General
    Replies: 3
    Last Post: 07-18-2006, 06:45 AM
  2. [SOLVED] Very Simple Question
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-18-2006, 09:55 PM
  3. Simple Question
    By cjjoo in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-12-2005, 12:05 AM
  4. [SOLVED] Simple Simple Excel usage question
    By BookerW in forum Excel General
    Replies: 1
    Last Post: 06-23-2005, 05:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1