Results 1 to 8 of 8

Thread: How is the behaviour of VB6 compiler while comparing two string?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Posts
    110

    How is the behaviour of VB6 compiler while comparing two string?

    Hi,

    I need some knowledge about 'how does vb6 compiled exe behave while comparing to strings with same length.

    Say String1="ABCDEF" and String2="ABCxEF"

    As you see, the difference is at 4th character (D -> x)

    When we try to compare these two strings (with is, select case etc)
    a) The compiler compares each characters one-by-one until encounter a difference. Once it encounter a difference (in
    this case at 4th character) it returns false immediately disregarding the rest of strings.

    or

    b) The compiler compare each characters from first to last and returns (true or false)


    I am asking this question because to have an idea about time consuming thning in a complied exe. Obviously, option a
    is faster then option b.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,506

    Re: How is the behaviour of VB6 compiler while comparing two string?

    No idea if the compiler optimizes a String-Compare to a RTLCompareMemory, but i wouldn't be surprised,
    if a "If String1=String2 Then" compares the whole memory-blocks the Stringpointers point to.
    Why don't you test it with really long Strings that differ at, say, the 23457th Position?
    Once with "String1=String2" vs "RTLCompareMemory(StrPtr(String1),StrPtr(String2))"
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  3. #3
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,020

    Re: How is the behaviour of VB6 compiler while comparing two string?

    This is CComBSTR implementation in ATL

    Code:
    bool operator==(_In_ const CComBSTR& bstrSrc) const throw()
    {
        return VarBstrCmp(m_str, bstrSrc.m_str, LOCALE_USER_DEFAULT, 0) == static_cast<HRESULT>(VARCMP_EQ);
    }
    Most probably the VB6 compiler it emitting a call to its own intrinsic function -- a custom wrapper in MSVBVM60 of the COM VarBstrCmp API function.

    cheers,
    </wqw>

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,506

    Re: How is the behaviour of VB6 compiler while comparing two string?

    ...which actually implies the same way RTLCompareMemory works: compare each "character" (honoring the flags), until there is a "difference"
    Otherwise it couldn't return Results like "is less" "is equal" or "is greater"
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  5. #5
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,020

    Re: How is the behaviour of VB6 compiler while comparing two string?

    Quote Originally Posted by Zvoni View Post
    ...which actually implies the same way RTLCompareMemory works: compare each "character" (honoring the flags), until there is a "difference"
    Otherwise it couldn't return Results like "is less" "is equal" or "is greater"
    What else can it be besides some crypto constant-time implementation?

    cheers,
    </wqw>

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,506

    Re: How is the behaviour of VB6 compiler while comparing two string?

    Quote Originally Posted by wqweto View Post
    What else can it be besides some crypto constant-time implementation?

    cheers,
    </wqw>
    Considering it's Microsoft we're talking about? *shrug*
    Out of my left sleeve: Take a binary representation of both strings, and subtract one from the other. If Result = 0 Then Equal Else NotEqual (ignoring flags for simplicity's sake)
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,543

    Re: How is the behaviour of VB6 compiler while comparing two string?

    Quote Originally Posted by ucanbizon View Post
    I am asking this question because to have an idea about time consuming thning in a complied exe.
    When talking about equality/inequality, i.e., Str1=Str2 or Str1<>Str2, then first test should probably be string length. If strings are not the same size; then strings cannot be equal.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,506

    Re: How is the behaviour of VB6 compiler while comparing two string?

    Quote Originally Posted by LaVolpe View Post
    When talking about equality/inequality, i.e., Str1=Str2 or Str1<>Str2, then first test should probably be string length. If strings are not the same size; then strings cannot be equal.
    True, but i wouldn't be surprised if under the hood it's the same function every time, overloaded with flags and what not. see Flag IGNOREWIDTH in wqweto's link, which basically can be used for "starts with"-comparisons using RTLCompareMemory

    EDIT: Sorry, just saw that RTLCompareMemory doesn't take flags, meaning you'd have to do anything yourself beforehand before sending it to the API (Case-Insensitive etc.)
    Last edited by Zvoni; Nov 18th, 2020 at 10:05 AM.
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

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