Results 1 to 4 of 4

Thread: [RESOLVED] IIF Statement

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Resolved [RESOLVED] IIF Statement

    Would you say these two produce the same result, all the time, when testing for booleans?

    blnResult = (CDate(strDate) > Now)

    blnResult = IIf(CDate(strDate) > Now, True, False)
    Last edited by Liquid Metal; Jan 31st, 2008 at 12:33 PM.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: IIF Statement

    I would say that they're functionally equivalent.

    (CDate(strDate) > Now) will return True or False as will the IIf statement.

    Perhaps the IIf statement is a little more obvious, to the reader, about what's being tested, but that's a matter of interpretation.

    Just out of interest I timed the two. Executing a 1,000,000 of each resulted in the following times on my machine:
    Code:
        binResult = (CDate(strDate) > Now) - about 7,500 mS
        binResult = IIf(CDate(strDate) > Now, True, False) - about 8,300 mS

    Any particular reason you're asking?

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: IIF Statement

    Quote Originally Posted by Doogle
    I would say that they're functionally equivalent.

    (CDate(strDate) > Now) will return True or False as will the IIf statement.

    Perhaps the IIf statement is a little more obvious, to the reader, about what's being tested, but that's a matter of interpretation.

    Just out of interest I timed the two. Executing a 1,000,000 of each resulted in the following times on my machine:
    Code:
        binResult = (CDate(strDate) > Now) - about 7,500 mS
        binResult = IIf(CDate(strDate) > Now, True, False) - about 8,300 mS

    Any particular reason you're asking?
    IIF statement would obviously be slow because it is a function and would evaluate all its input parameters irrespective of it's type.

    Besides this, the overhead of transfer of control from and to the function would also be significant in this situation as the first statement is just a simple mathematical comparison while the other one is a function call.

    Pradeep
    Last edited by Pradeep1210; Jan 31st, 2008 at 06:59 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: IIF Statement

    Thanks to both of you Doogle and PraDeep for answering my question!

    Doogle, also thanks for taking it to another level and did that speed testing for me. No particular reason why I want to know but I just like to improve and improve on writing efficient code. That is all.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

Posting Permissions

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



Click Here to Expand Forum to Full Width