Re: copymemory basic question
- From: "Donald Lessau" <don@xxxxxxxxxxxxxx>
- Date: Wed, 31 Aug 2005 21:59:22 +0200
"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
.
- Follow-Ups:
- Re: copymemory basic question
- From: Karl E. Peterson
- Re: copymemory basic question
- References:
- Re: copymemory basic question
- From: Karl E. Peterson
- Re: copymemory basic question
- From: Donald Lessau
- Re: copymemory basic question
- From: Jim Mack
- Re: copymemory basic question
- Prev by Date: Re: copymemory basic question
- Next by Date: Re: copymemory basic question
- Previous by thread: Re: copymemory basic question
- Next by thread: Re: copymemory basic question
- Index(es):
Relevant Pages
|
Loading