More Faster, More VB User

xiaoxiao2021-03-06  41

More Faster, More VB User

--VB bit operation

Why don't you add a grammatical element that doesn't add a bit operation in VB, I think the Bill's computer is hundreds of times faster than most people's computers. He may feel enough.

- llib.setag

This article is used to solve the problem of VB median operations, I think. In fact, this article is a bit too much, because many of the code is not my original, although many of these technologies have already been used there much, but in place operation, especially using arrays to perform high and low tabs (bytes) I didn't think of it, this is the credit of 9cbs qiqi5521, thank him for his code.

Three techniques are mainly used in the code:

Array technology, pointer technology, embedded assembly technology

These three technologies are actually not what we have discovered, essentially from the method ranked from "Hardcore VB" and "Advanced Visual Basic". Although these three technologies will not be mentioned in the general VB teaching book, because it has certain practicality, in many places, especially if the speed, the speed can be accelerated. With regard to specific technologies, I don't want to talk in this article. If you are interested, you can refer to my blog: http:/blog.9cbs.net/boedog, there is a "VB depth dive" series, although there is no After writing, these technologies will be mentioned.

Code, code, or code, for programmers, code is the best teaching book. The code is our way to survive. However, before the code, there are a few words to say, including a few versions of code in this module, is a bit chaos, which are general speed, higher speed and maximum speed. In addition, there are two functions in the code to remember to use, namely

Bitoperatorinit and Bitoperatore, these two functions are used before and after bit operation. This module references WinAPI ANSI TYPLIB.

Come, the code is coming.

Create a Modbit module to copy the following code.

Option expedition

'Version update record

'

'2004-12-23 Increase Using Memory Sharing Development New Function Hibytefastest ......

'This version of the new function is improved by using the code of qiqi5521 on 9CBS.

'

PRIVATE TYPE BIT_WORD

Lobyte as Byte

Hibyte as Byte

End Type

PRIVATE TYPE BIT_DWORD

Loword as integer

HiWord As Integer

End Type

Private type bit_dword_byte

Lobyte as Byte

Sebyte as Byte

THBYTE AS BYTE

Hibyte as Byte

End Type

Private Bitpower (31) As long '0-31

Private Bitright32code (23) as Byte

Private Shr32Addr as long 'assembly function address

Private Bitleft32code (23) as Byte

Private SHL32ADDR AS long 'assembly function address

Private Type SafeArray1D '1 dimensional number of SafeArray definitions

CDIMS as integer 'dimension

FFEATURES AS INTEGER 'Sign

Cbelements as long 'bytes of single elements

Clocks as long 'lock count

PVDATA as long 'points to array elements

CELEMENTS As long 'dimension, the number of elements of this dimension LBound As long' this dimension

End Type

Const fadf_auto = & h1

Const fadf_fixedsize = & h10

Private Declare Function Varptrarray Lib "msvbvm60.dll" Alias ​​"Varptr" (PTR () AS ANY) AS LONG

Private M_SA1DLONG As SafeArray1d

Private M_SA1DINT AS SAFEARRAY1D

Private m_lsharedlong as long 'to be shared

Private M_AIINTSINLONG () AS INTEGER 'To share a shaping array of long plastic address space

Private m_lsharedint AS INTEGER

Private m_aibytesinint () as Byte

'

The first initialization of the 'bit operation is mainly the initialization, and if the initialization is not performed, it will not be expected to be wrong.

Public Sub Bitoperatorinit ()

DIM I as long

DIM Asmcodebyte

FOR i = 0 to 30

BitPower (i) = 2 ^ i

NEXT

Bitpower (31) = & h80000000

AskCODEBYTE = Array (_

& H55, & H8B, & HEC, & H83, & HEC, & H44, & H53, & H56 ,_

& H57, & H8B, & H45, & H8, & H8B, & H4D, & HC, & HD3 ,_

& HE8, & H5F, & H5E, & H5B, & H8B, & HE5, & H5D, & HC3)

FOR i = 0 to 23

Bitright32code (i) = asmcodebyte (i)

NEXT

SHR32ADDR = Varptr (Bitright32code (0))

AskCODEBYTE = Array (_

& H55, & H8B, & HEC, & H83, & HEC, & H44, & H53, & H56 ,_

& H57, & H8B, & H45, & H8, & H8B, & H4D, & HC, & HD3 ,_

& HE0, & H5F, & H5E, & H5B, & H8B, & HE5, & H5D, & HC3)

FOR i = 0 to 23

BitLeft32code (i) = asmcodebyte (i)

NEXT

SHL32ADDR = VARPTR (Bitleft32code (0))

WITH M_SA1DLONG

.cdims = 1

.ffeatures = 17

.cbelements = 2

.clocks = 0

.pvdata = varptr (m_lsharedlong) 'makes the array data pointer points to long oandid variables m_lsharedlong

.CELEMENTS = 2

.Lbound = 0

End with

'Make array variables (actually a pointer) point to the SafeArray1D structure you created yourself.

CopyMemory Byval Varptrarray (m_aiinsinlong), Varptr (m_sa1dlong), 4with M_SA1DINT

.cdims = 1

.ffeatures = 17

.cbelements = 1

.clocks = 0

.pvdata = varptr (m_lsharedint) 'makes the array data pointer points to long shape variables m_lsharedlong

.CELEMENTS = 2

.Lbound = 0

End with

CopyMemory Byval Varptrarray (M_Aibytesinint), Varptr (M_SA1DINT), 4

End Sub

'After the bit operation operation is completed, this function is to call this function.

'Release the resource, be called before the program

Public Sub Bitoperatorend ()

'Point 0 to NULL in C-language (in fact, a pointer)

CopyMemory Byval Varptrarray (M_AIINTSINLONG), 0 &, 4

CopyMemory Byval Varptrarray (M_Aibytesinint), 0 &, 4

End Sub

'///

'Take the fastest version of the high level

Public Function HiWordfastest (Byref Num As Long) AS Integer

m_lsharedlong = NUM

HiWordfastest = m_aiintsinlong (1)

END FUNCTION

'Take the fastest version of the low level

Public Function Lowordfast (Byref Num As Long) AS Integer

m_lsharedlong = NUM

LOWORDFASTEST = M_AIINTSINLONG (0)

END FUNCTION

'Take the fastest version of the high bYTE

Public Function Hibytefastest (Byref Num As Integer) AS Integer

m_lsharedint = Num

Hibytefastest = m_aibytesinint (1)

END FUNCTION

'Take the fastest version of the low level

Public Function LobyTefastest (byref Num As Integer) AS Integer

m_lsharedint = Num

LobyTefastest = M_Aibytesinint (0)

END FUNCTION

'///

'Bit test, test bit is 1 return true

Public Function BitTest32 (Number As Long, Bit As Boolean) AS Boolean

If Number and Bitpower (Bit) THEN

Bittest32 = true

Else

Bittest32 = false

END IF

END FUNCTION

'Set a bit to 1 and return the value

Public Function BitSet32 (Number As Long, Bit As Long) AS LONG

BitSet32 = BitPower (bit) or Number

END FUNCTION

'No symbol extension, it is best to reducing call expenses as an embedded function. Note that it has essentially the difference from CLNG

Public Function Word2dWord (Num as Integer) AS Long

Word2dword = Num and & HFFFF &

End function 'logic right shift 32-bit function

Public Function SHR32 (BYREF NUM AS Long, Bitnum As Long) As long

SHR32 = CallWindowProc (Byval SHR32ADDR, Num, Bitnum, 0, 0)

END FUNCTION

'Logical left shift 32-bit function

Public Function SHL32 (BYREF NUM AS Long, Bitnum As Long) As long

SHL32 = CallWindowProc (Byval SHL32ADDR, NUM, BITNUM, 0, 0)

END FUNCTION

'No assembly, if you need it, but 16 bits of assembly code is much different from 32 bits, this function speed is more slow than SHR32.

Public Function SHR16 (BYREF NUM AS INTEGER, BITNUM AS Long) AS Integer

DIM MDW As Bit_dword

DIM RL As Long

mdw.loword = Num

CopyMemory RL, MDW, 4

RL = CallWindowProc (Byval SHR32ADDR, RL, BITNUM, 0, 0)

CopyMemory SHR16, RL, 2

END FUNCTION

Public Function SHL16 (BYREF NUM AS INTEGER, BITNUM AS Long) AS Integer

DIM MDW As Bit_dword

DIM RL As Long

mdw.loword = Num

CopyMemory RL, MDW, 4

RL = CallWindowProc (Byval SHL32ADDR, RL, BitNum, 0, 0)

CopyMemory SHL16, RL, 2

END FUNCTION

'///

'If there is not much demanding for performance, or it is not possible to understand the following functions, you can use the following functions.

'General is not recommended

Public Function Hibyte (byref Num as Integer) AS Byte

DIM MW As Bit_word

CopyMemory MW, Num, 2

Hibyte = mw.hibyte

END FUNCTION

Public Function Lobyte (Byref Num As Integer) AS Byte

CopyMemory Lobyte, Num, 1 'can save a value,

END FUNCTION

Public Function HiWord (Byref Num As Long) AS Integer

DIM MDW As Bit_dword

CopyMemory MDW, NUM, 4

HiWord = MDW.HIWORD

END FUNCTION

Public Function Loword (Byref Num As Long) AS Integer

CopyMemory Loword, Num, 2

END FUNCTION

'Faster than Loword (but if built into a large function, you may be better with loword, because the code is clearer)

Public Function Lowordfaster (Byref Num As Long) AS Integer

If (Num and & H8000 &) THEN 'DW AND & H00008000 to determine the low position is negative

LOWORDFASTER = Num or & hffff0000

Else

LOWORDFASTER = Num and & hfff & 'dw and & h0000ffff

END IF

END FUNCTION

转载请注明原文地址:https://www.9cbs.com/read-77304.html

New Post(0)