Re: copymemory basic question
- From: "Karl E. Peterson" <karl@xxxxxxxx>
- Date: Wed, 31 Aug 2005 16:11:35 -0700
Jim Mack wrote:
> Karl E. Peterson wrote:
>>>> I might have an idea on speeding that up a bit...
>>>
>>> Just for completeness, there's a DxSwapStr function in Stamina --
>>> worth seeing how it stacks [cough] up against any VB versions.
>>
>> <ahem>
>>
>> Well... Yeah, that one pretty well blows away the immediate
>> competition. Three runs, VB5/SP3, EXE /w all opts:
>
> I took a look at the source, and I see that I'm being overly paranoid
> about what might get passed in. I could cut the code by 1/3 if I
> could guarantee no null pointers, and I think there may be a way to
> do that. Probably get down to 75 on your scale.
>
> But I'll only do that if you find a way to top it. :-)
I think you got your work cut out for you. Latest results:
Iterations: 10000000
Overhead: 11
SwapStr01: 3714 (1.000)
SwapStr02: 1208 (0.325)
SwapStr03: 1238 (0.333)
SwapStr04: 1187 (0.320)
DxSwapStr: 920 (0.248)
SwapStr05: 592 (0.159)
SwapStr06: 366 (0.099)
SwapStr01: 3797 (1.000)
SwapStr02: 1237 (0.326)
SwapStr03: 1273 (0.335)
SwapStr04: 1239 (0.326)
DxSwapStr: 989 (0.260)
SwapStr05: 640 (0.169)
SwapStr06: 385 (0.101)
SwapStr01: 3841 (1.000)
SwapStr02: 1257 (0.327)
SwapStr03: 1255 (0.327)
SwapStr04: 1204 (0.313)
DxSwapStr: 932 (0.243)
SwapStr05: 612 (0.159)
SwapStr06: 377 (0.098)
Wanna see the code? <bSEg>
Public Sub SwapStr01(s1 As String, s2 As String)
Dim tmp As String
' Traditional method using pure VB.
tmp = s1
s1 = s2
s2 = tmp
End Sub
Public Sub SwapStr02(s1 As String, s2 As String)
Static lpTmp As Long
' Basic CopyMemory method that swaps pointers, using
' a Static variable for temporary holding space.
lpTmp = StrPtr(s1)
Call CopyMemory(ByVal VarPtr(s1), ByVal VarPtr(s2), 4&)
Call CopyMemory(ByVal VarPtr(s2), lpTmp, 4&)
End Sub
Public Sub SwapStr03(s1 As String, s2 As String)
Dim lpTmp As Long
' Basic CopyMemory method that swaps pointers, using
' a standard variable for temporary holding space.
lpTmp = StrPtr(s1)
Call CopyMemory(ByVal VarPtr(s1), ByVal VarPtr(s2), 4&)
Call CopyMemory(ByVal VarPtr(s2), lpTmp, 4&)
End Sub
Public Sub SwapStr04(s1 As String, s2 As String)
Dim ptrStr1 As Long
Dim ptrVar2 As Long
' Enhanced CopyMemory removes one VarPtr call, and
' doesn't use Static vars for pointers.
ptrStr1 = StrPtr(s1)
ptrVar2 = VarPtr(s2)
CopyMemory ByVal VarPtr(s1), ByVal ptrVar2, 4&
CopyMemory ByVal ptrVar2, ptrStr1, 4&
End Sub
Public Sub SwapStr05(s1 As String, s2 As String)
Static lpTmp As Long
' Use PutMem4 and GetMem4 directly from runtime.
' This method requires no typelibs!
lpTmp = StrPtr(s1)
'Call GetMem4(VarPtr(s1), lpTmp)
Call PutMem4(VarPtr(s1), StrPtr(s2))
Call PutMem4(VarPtr(s2), lpTmp)
End Sub
Public Sub SwapStr06(s1 As String, s2 As String)
Static lpTmp As Long
' Use typelib'd versions of PutMem4 and GetMem4.
lpTmp = AsLng(s1) ' StrPtr(s1)
AsLng(s1) = AsLng(s2)
AsLng(s2) = lpTmp
End Sub
These are the declarations:
' ** Defined in CopyMem.tlb, to avoid GetLastError overhead
' Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination
As Any, Source As Any, ByVal Length As Long)
Private Declare Sub GetMem4 Lib "msvbvm50" (ByVal Addr As Long, RetVal As Long)
Private Declare Sub PutMem4 Lib "msvbvm50" (ByVal Addr As Long, ByVal NewVal As
Long)
I'm going to put the typelib out for download, but want to make sure I run it through
its paces first. It was inspired by the work of Michel Rutten at:
http://www.xbeat.net/vbspeed/i_VBVM6Lib.html
In a nutshell, here's what I did for AsLng:
// ========================================================================
// Property AsLng
// ========================================================================
[
entry("GetMem4"), propget,
helpstring("Returns or sets the value referenced by the source parameter cast to a
Long.")
]
HRESULT __stdcall AsLng(
[in] void * Source,
[out, retval] long * lpRetVal
);
[
entry("PutMem4"), propput
]
HRESULT __stdcall AsLng(
[in] void * Source,
[in] long NewValue
);
I'll follow-up in another venue, with more testing/debugging info.
Thanks... Karl
--
Working Without a .NET?
http://classicvb.org/petition
.
- Follow-Ups:
- Re: copymemory basic question
- From: Kevin Provance
- Re: copymemory basic question
- From: Donald Lessau
- Re: copymemory basic question
- From: Jim Mack
- 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
- From: Donald Lessau
- Re: copymemory basic question
- From: Karl E. Peterson
- Re: copymemory basic question
- From: Jim Mack
- Re: copymemory basic question
- From: Karl E. Peterson
- 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