Several function speed comparison of HEXTOINT

xiaoxiao2021-03-05  28

Unit Unit1

;

Interface Uses Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms

Dialogs, stdctrls

;

TYPE TFORM1 = Class (TFORM

Button1: TButton

; MEMO1: TMEMO

Procedure Button1Click (sender: TOBJECT

);

Private

{Private Declarations}

public

{Public declarations} END

;

Var Form1: TFORM1

;

IMPLEMentation

{$ R * .dfm}

Const HexStr: String = 'Abcd1234'

CALCTIMES: Integer = 1000000

;

TYPE THEXTOINTFUNC = Function (const s: string): DWORD

Function HEXTOINT_TSEUG0 (Const S: String): DWORD

;

ASM

Push EBX PUSH ESI MOV ESI, EAX

// String address MOV EDX, [EAX-4]

// Read the string length xor Eax, EAX

// Initialization return value xor ECX, ECX

// Temporary Variable Test ESI, ESI

/ / Judgment whether it is an empty pointer

JZ @@ 2 Test Edx, EDX

/ / Judgment whether the string is empty

Jle @@ 2 MOV BL, $ 20 @@ 0: MOV CL, [ESI] Inc ESI or Cl, BL

// If there is a letter, it is converted to lowercase letters.

SUB CL, '0' JB @@ 2

// <'0' characters

CMP CL, $ 09 JBE @@ 1

// '0' .. '9' characters

SUB CL, 'A' - '0'-10 CMP CL, $ 0A JB @@ 2

// <'a' characters

CMP CL, $ 0f ja @@ 2

//> 'f' character @@ 1:

// '0' .. '9', 'a' .. 'f', 'a' .. 'f'

SHL EAX, 4 or Eax, ECX DEC EDX JNZ @@ 0 JMP @@ 3 @@ 2: xor Eax, EAX

// illegal 16 credit string

@@ 3: POP ESI POP EBX RET END

Function HEXTOINT_TSEUG1 (const s: string): DWORD

;

Var i: integer

;

Begin Result: = 0

; For i: = 1 to Length (s)

Do Begin Case S [I]

Of '0' .. '9': Result: = Result * 16 ORD (s [i]) - ORD ('0'

); 'A' .. 'f': result: = result * 16 ORD (S [i]) - ORD ('a') 10; 'a' .. 'f': Result: = Result * 16 ORD (S [i]) - ORD ('A') 10

;

Else Result: = 0

EXIT

; END

;

End end

Function HEXTOINT_DOUBLEWOOD (const S: string): DWORD

;

Const Convert: array [0..255] of integer

= (-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1

, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1

, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1

, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1

-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1

, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1

-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1

, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1

, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1

, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1

, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1

, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1

, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1

, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1

, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1

, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -

1

);

Var i: integer

; V: INTEGER

;

Begin Result: = 0

; If pointer (s) = nil dam

; For i: = 1 to Pinteger (Integer (s) - 4) ^

Do Begin Begin V: = Convert [ORD (S [i

])])]); If v <0

Then Begin Result: = 0

EXIT

; END

Result: = (Result * 16) or v

; END

; END

; END

Function HEXTOINT_BETA1 (Const S: String): DWORD

;

Const ValidateTBL: Array [0..255] of byte = (16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 16, 16, 16, 16, 16, 16

, 16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

);

ASM

Push EBX PUSH ECX Push Edx Push ESI Push EDI MOV ESI, EAX

// String address MOV ECX, [EAX-4]

// Read the string length Test ESI, ESI

/ / Judgment whether it is an empty pointer

JZ @err test ECX, ECX

/ / Judgment whether the string is empty

Jle @err xor EAX, EAX LEA EDI, VALIDATETBL MOV EDX, ECX XOR EBX, EBX @LOOPVALIDATE: MOV BL, [ESI] MOV BL, [EDI] [EBX] Test EBX, 16 JNZ @err SHL EAX, 4 or EAX , EBX INC ESI DEC EDX JNZ @LoopValidate JMP @Ext @err: xor Eax, EAX

// illegal 16 credit string

@Ext: Pop Edi Pop ESI POP EDX POP ECX POP EBX END

Function HEXTOINT_BETA2 (Const S: String): DWORD;

Const ValidateTbl: array [0..255] of byte

= (16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 16, 16, 16, 16, 16, 16

, 16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16

);

ASM

Push EBX PUSH ECX PUSH EDX PUSH ESI PUSH EDI MOV EDI, ESP PUSH 0 MOV ESI, EAX

// String address MOV ECX, [EAX - 4]

// Read the string length Test ESI, ESI

/ / Judgment whether it is an empty pointer

JZ @err test ECX, ECX

/ / Judgment whether the string is empty

Jle @err lea eax, validatetbl xor Edx, Edx XOR EBX, EBX TEST ECX, 1 JZ @LEFTBYTES

// firstbyte:

MOV BL, [ESI] MOV BL, [EAX] [EBX] Test EBX, 16 JNZ @err shl BL, 4 DEC EDI MOV [EDI], BL DEC ECX JZ @Ext @leftbytes: MOV BL, [ESI] MOV BL , [EAX] [EBX] TEST EBX, 16 JNZ @err Mov DL, BL SHL DL, 4 Inc ESI MOV BL, [ESI] MOV BL, [EAX] [EBX] Test EBX, 16 JNZ @err or BL, DL Dec EDI MOV [EDI], BL INC ESI DEC ECX DEC ECX JNZ @LEFTBYTES JMP @Ext @err: MOV [ESP], 0 @ext: Pop Eax Pop EDI POP ESI POP EDX POP ECX POP EBX END;

// Test function, let the formulated calculation method multiple times to pull the time gap function testhext (HEXTOINTFUNC: THEXTOINTFUNC; VAR value: DWORD): DWORD

;

Var i: integer

;

Begin Result: = GettickCount

; For i: = 1 to CALCTIMES

Do value: = HEXTOINTFUNC (HEXSTR

Result: = GettickCount - Result

; END

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT

);

Var value: DWORD

;

Begin Memo1.Lines.Add (Format ('HEXTOINT_TSEUG0 (% DMS).', [TesthextOint (HEXTHEXTOINT (HEXTHEXTOINT (HEXTHEXTOINT (HEXTHEXTOINT

)])); Memo1.Lines.Add (Format ('HEXTOINT_TSEUG1 (% DMS).', [TESTHEXTOINT (HEXTOINT_TSEUG1, VALUE

)])); Memo1.Lines.Add (Format ('HEXTOINT_DOUBLEWOOD (% DMS).', [TesthextOint (HEXTHEXTOINT (HEXTOINT_DOUBLEWOOD, VALUE

)])); Memo1.Lines.Add (Format ('HEXTOINT_BETA1 (% DMS).', [TESTHEXTOINT (HEXTOINT_BETA1, VALUE

)])); Memo1.Lines.Add (Format ('HEXTOINT_BETA2 (% DMS).', [TESTHEXTOINT (HEXTOINT_BETA2, VALUE

)]); Memo1.Lines.Add ('1234'

); END

END.

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

New Post(0)