Re: copymemory basic question



On a second thought, it seems that the optimizer in VB copies the pointers
to strings instead of swapping the contents. I didn't know that. It seems
that it also keeps track of the last few strings used, even if you assigned
a different value. However, I don't think that it's a good idea to rely on
whether the optimizer can do it or not, it's best to test it with a timing
routine to see which method is faster. Even with this optimization, it might
be slower than "Swapping arrays of Longs representing the Indexes of
strings", because it seems that VB uses internal memory management functions
when it does this during runtime.


Here is a test sample I did and its output:

Private Sub Form_Click()
Dim s1 As String
Dim s2 As String
Dim temp As String

s1 = "abc"
s2 = "def"
temp = ""

' Set some form properties so the output can be seen clearly when in
EXE.
Me.AutoRedraw = True
Me.FontName = "Courier"

Debug.Print " s1", , " s2", , " temp"
Debug.Print "Before: "; VarPtr(s1); StrPtr(s1), VarPtr(s2);
StrPtr(s2), VarPtr(temp); StrPtr(temp)

temp = s1
Debug.Print "After temp = s1: "; VarPtr(s1); StrPtr(s1), VarPtr(s2);
StrPtr(s2), VarPtr(temp); StrPtr(temp)
s1 = s2
Debug.Print "After s1 = s2: "; VarPtr(s1); StrPtr(s1), VarPtr(s2);
StrPtr(s2), VarPtr(temp); StrPtr(temp)
s2 = temp

Debug.Print "After s2 = temp: "; VarPtr(s1); StrPtr(s1), VarPtr(s2);
StrPtr(s2), VarPtr(temp); StrPtr(temp)

End Sub



OUTPUT(Copy to Notepad to see it clearly):


s1 s2 temp
Before: 1308792 1530892 1308788 2193116
1308784 1530932
After temp = s1: 1308792 1530892 1308788 2193116
1308784 2217996
After s1 = s2: 1308792 1530932 1308788 2193116
1308784 2217996
After s2 = temp: 1308792 1530932 1308788 1530892
1308784 2217996

My environment: Windows XP with SP2, and VB6 with SP5.


.



Relevant Pages

  • String Concat vs. Plus operator
    ... Dumping the IL for TestStringOp reveals that this is converted to the ... the strings into it before finally calling the fast string allocator. ... concat is 4 or less. ... Understandably timing this code won't be very revealing since the optimizer ...
    (microsoft.public.dotnet.framework)
  • String swapping problem
    ... I tried the following code for swapping a string, ... Inside the swap function the strings are ... char * temp; ...
    (comp.lang.c)
  • Re: beginner string question
    ... > a compile error. ... Use std::string instead of plain C-style strings. ... If you declared temp ... result of comparing will be always false. ...
    (comp.lang.cpp)
  • Re: beginner string question
    ... Sorry but I gave the wrong info, I declared "temp" as a character array if ... I did use strings but they were breaking up in ... > a compile error. ...
    (comp.lang.cpp)
  • Re: String() declaration?
    ... the temp array is holding strings ... Dim temp as String ...
    (microsoft.public.dotnet.languages.vb)