'VB内嵌ASM加快内存数据复制
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes As Long)
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function QueryPerformanceCounter Lib "kernel32" (X As Currency) As Boolean
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (X As Currency) As Boolean
Private OpCode(200) As Byte, CodeStar As Long, opIndex As Long
Private sArr(5000000) As Byte, dArr(5000000) As Byte
'*************************************************************************
'**模 块 名:SuperCopyMemory VB范例
'**说 明:丹心软件在线设计 版权所有2007 - 2008(C)
'**创 建 人:丹心
'**日 期:2007-09-03 22:13:43
'**修 改 人:
'**日 期:
'**描 述:比CopyMemory还要快的函数SuperCopyMemory(),应用在高速内存复制需求上
'**版 本:V1.0.0
'**博客地址:http://hi.baidu.com/starwork/
'**QQ 号码:121877114
'**E - mail:cnstarwork@126.com
'*************************************************************************
Public Sub SuperCopyMemory(ByVal lpDest As Long, ByVal lpSource As Long, ByVal cBytes As Long)
CallWindowProc CodeStar, 0, lpDest, lpSource, cBytes
End Sub
Public Sub AsmIni()
Dim i As Long
CodeStar = (VarPtr(OpCode(0)) Or &HF) + 1
opIndex = CodeStar - VarPtr(OpCode(0))
For i = 0 To opIndex - 1
OpCode(i) = &HCC
Next
AddByteToCode &H50: AddByteToCode &H53: AddByteToCode &H51: AddByteToCode &H56: AddByteToCode &H57: AddByteToCode &H8B
AddByteToCode &H7C: AddByteToCode &H24: AddByteToCode 28: AddByteToCode &H8B: AddByteToCode &H74: AddByteToCode &H24
AddByteToCode 32: AddByteToCode &H8B: AddByteToCode &H4C: AddByteToCode &H24: AddByteToCode 36: AddByteToCode &HB8
i = 64
AddLongToCode i: AddByteToCode &H8B: AddByteToCode &HD9: AddByteToCode &HFC: AddByteToCode &H3B: AddByteToCode &HC8
AddByteToCode &H7C: AddByteToCode &H52: AddByteToCode &HC1: AddByteToCode &HE9: AddByteToCode &H6: AddByteToCode &HF
AddByteToCode &H18: AddByteToCode &H46: AddByteToCode &H40: AddByteToCode &HF: AddByteToCode &H18: AddByteToCode &H47
AddByteToCode &H40: AddByteToCode &HF: AddByteToCode &H6F: AddByteToCode &H6: AddByteToCode &HF: AddByteToCode &HE7: AddByteToCode &H7
AddByteToCode &HF: AddByteToCode &H6F: AddByteToCode &H4E: AddByteToCode &H8: AddByteToCode &HF: AddByteToCode &HE7
AddByteToCode &H4F: AddByteToCode &H8: AddByteToCode &HF: AddByteToCode &H6F: AddByteToCode &H56: AddByteToCode &H10
AddByteToCode &HF: AddByteToCode &HE7: AddByteToCode &H57: AddByteToCode &H10: AddByteToCode &HF
AddByteToCode &H6F: AddByteToCode &H5E: AddByteToCode &H18: AddByteToCode &HF: AddByteToCode &HE7: AddByteToCode &H5F
AddByteToCode &H18: AddByteToCode &HF: AddByteToCode &H6F: AddByteToCode &H66: AddByteToCode &H20: AddByteToCode &HF
AddByteToCode &HE7: AddByteToCode &H67: AddByteToCode &H20: AddByteToCode &HF: AddByteToCode &H6F
AddByteToCode &H6E: AddByteToCode &H28: AddByteToCode &HF: AddByteToCode &HE7: AddByteToCode &H6F: AddByteToCode &H28
AddByteToCode &HF: AddByteToCode &H6F: AddByteToCode &H76: AddByteToCode &H30: AddByteToCode &HF: AddByteToCode &HE7
AddByteToCode &H77: AddByteToCode &H30: AddByteToCode &HF: AddByteToCode &H6F: AddByteToCode &H7E
AddByteToCode &H38: AddByteToCode &HF: AddByteToCode &HE7: AddByteToCode &H7F: AddByteToCode &H38
AddByteToCode &H3: AddByteToCode &HF0: AddByteToCode &H3: AddByteToCode &HF8: AddByteToCode &H49
AddByteToCode &H75: AddByteToCode &HB3: AddByteToCode &HF
AddByteToCode &H77: AddByteToCode &H8B: AddByteToCode &HCB: AddByteToCode &H48: AddByteToCode &H23
AddByteToCode &HC8: AddByteToCode &H74: AddByteToCode &H2: AddByteToCode &HF3: AddByteToCode &HA4: AddByteToCode &H5F
AddByteToCode &H5E: AddByteToCode &H59: AddByteToCode &H5B: AddByteToCode &H58: AddByteToCode &HC2
AddByteToCode &H10: AddByteToCode &H0: AddByteToCode &HCC
End Sub
Public Sub AddByteToCode(bData As Byte)
OpCode(opIndex) = bData
opIndex = opIndex + 1
End Sub
Public Sub AddLongToCode(lData As Long)
CopyMemory OpCode(opIndex), lData, 4
opIndex = opIndex + 4
End Sub
'SuperCopyMemory() Function
Private Sub Command1_Click()
Dim S1 As Currency, S2 As Currency
Dim f As Currency, t1 As Currency
Call QueryPerformanceFrequency(f) '计时
Call QueryPerformanceCounter(S1)
SuperCopyMemory VarPtr(dArr(0)), VarPtr(sArr(0)), 5000000
Call QueryPerformanceCounter(S2)
t1 = (S2 - S1) / f
Command1.Caption = "时间=" & t1
End Sub
'----- CopyMemory
Private Sub Command2_Click()
Dim S1 As Currency, S2 As Currency
Dim f As Currency, t1 As Currency
Call QueryPerformanceFrequency(f) '计时
Call QueryPerformanceCounter(S1)
CopyMemory dArr(0), sArr(0), 5000000
Call QueryPerformanceCounter(S2)
t1 = (S2 - S1) / f
Command2.Caption = "时间=" & t1
End Sub
'----- 传统VB方式
Private Sub Command3_Click()
Dim S1 As Currency, S2 As Currency
Dim f As Currency, t1 As Currency
Dim i As Long
Call QueryPerformanceFrequency(f) '计时
Call QueryPerformanceCounter(S1)
For i = 0 To 4999999
dArr(i) = sArr(i)
Next
Call QueryPerformanceCounter(S2)
t1 = (S2 - S1) / f
Command3.Caption = "时间=" & t1
End Sub
Private Sub Form_Load()
Me.Caption = "SuperCopyMemory VB范例"
Dim i As Long, j As Integer
For i = 0 To UBound(sArr)
sArr(i) = j
j = j + 1
If j > 255 Then j = 0
Next
AsmIni
End Sub
丹心版权所有.转载发表请注明出处,并通知本人