Re: copymemory basic question



"Jim Mack" <jmack@xxxxxxxxxxxxxxx> schrieb im Newsbeitrag
news:%23u2$QMmrFHA.332@xxxxxxxxxxxxxxxxxxxxxxx
Donald Lessau wrote:
> "Karl E. Peterson" wrote...
>>
>> Private Sub SwapPtrs(s1 As String, s2 As String)
>> Static lpTmp As Long
>> lpTmp = StrPtr(s1)
>> Call CopyMemory(ByVal VarPtr(s1), ByVal VarPtr(s2), 4&)
>> Call CopyMemory(ByVal VarPtr(s2), lpTmp, 4&)
>> End Sub
>>
>
> Why Static?
>

>>>
I don't know if the VB compiler takes advantage of this, but a function with
no local variables can have a smaller prolog, which would make it faster.
Static locals are the way to avoid stack variables and still remain
structured.

A function like this, which presumably would be executed many times in a
sort routine, is a candidate for any such optimization.
<<<

I wouldn't expect these things to be notable in the context of 2 API calls
and 3 VarPtr calls, but I'm not crazy enough to make *any guesses* in this
thread of hell ;)..., so I timed it and the result is: the Static-version is
consistently (and, just to make sure, independently of the size of the
swapped strings) 15% slower than the non-Static-version! I have no idea why.
Anyway, this version is faster than both (only 2 VarPtr calls):

Public Sub SwapString09(ByRef a As String, ByRef b As String)
Dim ptrStrA As Long
Dim ptrVarB As Long
ptrStrA = StrPtr(a)
ptrVarB = VarPtr(b)
CopyMemory ByVal VarPtr(a), ByVal ptrVarB, 4&
CopyMemory ByVal ptrVarB, ptrStrA, 4&
End Sub

Don

.



Relevant Pages

  • Re: copymemory basic question
    ... >>> Private Sub SwapPtrs(s1 As String, ... the Static-version is consistently (and, just to make sure, ... > CopyMemory ByVal VarPtr, ByVal ptrVarB, 4& ...
    (microsoft.public.vb.winapi)
  • Re: copymemory basic question
    ... Static lpTmp As Long ... Call CopyMemory, lpTmp, 4&) End Sub ... I was getting one string to copy to another but only as many characters as the destinations string previously had. ...
    (microsoft.public.vb.winapi)
  • Project Error
    ... Private Declare Sub Sleep Lib "Kernel32" ... Dim strDataSrc As String ...
    (microsoft.public.vb.bugs)
  • Re: FTP CD command
    ... My code connects to a ftp site and the enumerates all content on that place ... Public Property UriAs String ... End Sub ... Dim listRequest As FtpWebRequest = CType, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: FTP CD command
    ... Private _Uri As String ... End Sub ... Dim listRequest As FtpWebRequest = CType, ... Public Sub UploadAsynch(ByVal fileName As String, ByVal uploadUrl As ...
    (microsoft.public.dotnet.languages.vb)

Loading