Delphi Experiment: Find the position and efficiency evaluation of the i-i string in the string

xiaoxiao2021-03-06  37

LW549

The nth position of the specified sub-string in the string is obtained, the efficiency is not high, and it is barely available. I feel interest, so I try to try it. The program is attached to the end, here is some explanations: 1, for quick write, no use of console methods should be used, but use the GUI mode; 2, the sample sample is to find a string containing four substrings , Check four locations separately. This is done in the Button1Click method. It calls Tests to perform specific tests to measure functions, the second appearance, the number of cycles is parameters; 3. Tests repeats each specific function in one cycle, at the same time for fair (Perhaps the previous function is a latted road-memory, high-speed buffer), this test is test_count, and finally outputs the average time each time; 4, after the early test, the LW549 code is not high, So give it a small number of cycles (one thousand times), so as not to cause the programs to be killed; other three thousand; The POSEX function added in Delphi 7; POSN_STRPOS: Use the StrPos function. Program output:

Search "Function Getnstringpos (N: Integer; Substring, Astring: String): Integer;" for "string"

1:

Substr Index: 1; Loop Count = 1000

Getnstringpos: Return 17; TIMING: 37.60 MS

Substr Index: 1; Loop Count = 100000

POSN_POS: RETURN 17; TIMING: 40.40 ms

POSN_POSEX: RETURN 17; TIMING: 15.60 MS

POSN_STRPOS: RETURN 22; TIMING: 37.80 MS

2:

Substr Index: 2; Loop Count = 1000

Getnstringpos: Return 42; TIMING: 96.80 MS

Substr Index: 2; Loop Count = 100000

POSN_POS: RETURN 42; TIMING: 81.20 MS

POSN_POSEX: RETURN 42; TIMING: 47.00 MS

POSN_STRPOS: RETURN 47; TIMING: 53.00 MS

3:

Substr Index: 3; Loop Count = 1000

Getnstringpos: Return 50; TIMING: 109.40 MS

Substr Index: 3; Loop Count = 100000

POSN_POS: Return 50; TIMING: 118.80 MS

POSN_POSEX: RETURN 50; TIMING: 53.00 MS

POSN_STRPOS: RETURN 55; TIMING: 62.60 MS

4:

Substr Index: 4; Loop Count = 1000

Getnstringpos: Return 58; TIMING: 128.20 MS

Substr Index: 4; Loop Count = 100000

POSN_POS: RETURN 58; TIMING: 162.60 MS

POSN_POSEX: RETURN 58; TIMING: 59.40 MS

POSN_STRPOS: RETURN 63; TIMING: 74.80 MS It can be seen that the results of the test (efficiency) are: POSN_POSEX> POSN_STRPOS> POSN_POS >> GetnsUbstringPOS. I have expected that POSN_STRPOS is most powerful, but the result is not. It is estimated that POSEX optimization is relatively powerful. Access code:

Unit1.pas:

Unit unit1;

Interface

Uses

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

Dialogs, Stdctrls, DB, DBTABLES;

Type

Tposnfunc = function (n: integer; const Substring, Astring: String): Integer;

Type

TFORM1 = Class (TFORM)

Memo1: TMEMO;

Button1: tbutton;

Procedure Button1Click (Sender: TOBJECT);

Procedure formcreate (Sender: TOBJECT);

Private

Procedure Tests (Const Funcs: array of tposnfunc

Const funcnames: array of string; istr: integer; loopcount: integer;

public

END;

VAR

FORM1: TFORM1;

IMPLEMentation

Uses

Strustils;

{$ R * .dfm}

Function GetnstringPOS (N: Integer; Substring, Astring: String): Integer;

/ / Return the location of the nth subString appears in Astring

// If you didn't find it, return -1

VAR

FINDCOUNT: Integer;

POS: integer;

Begin

RESULT: = -1;

POS: = 0;

For FINDCOUNT: = 1 to n Do Begin

INC (POS);

While Midstr (Astring, Pos, Length (Substring) <> Substring Do Begin

If Length (Astring)

Exit; // Not found

INC (POS);

END;

END;

Result: = POS;

END;

Function POSN_POS (N: Integer; Substring, Astring: String): Integer;

VAR

p: integer;

NSUB: Integer;

NSRC: Integer;

Begin

NSUB: = Length (Substring);

NSRC: = Length (astring);

Result: = -nsub;

While n> 0 do

Begin

P: = POS (Substring, Astring);

IF P = 0 THEN

Break;

DEC (n);

INC (Result, P NSUB);

Astring: = COPY (Astring, P NSUB 1, NSRC-NSUB-P-1);

DEC (NSRC, NSUB P);

END;

IF n> 0 THEN

RESULT: = -1;

END;

Function POSN_POSEX (N: Integer; Substring, Astring: String): Integer; Var

p: integer;

NSUB: Integer;

Begin

NSUB: = Length (Substring);

Result: = 0;

P: = 0;

While n> 0 do

Begin

P: = posex (Substring, Astring, P 1);

IF P = 0 THEN

Break;

DEC (n);

Result: = P;

INC (P, NSUB);

END;

IF n> 0 THEN

RESULT: = -1;

END;

Function POSN_STRPOS (N: Integer; Substring, Astring: String): Integer;

VAR

PSUB, PSRC, P: PCHAR;

NSUB: Integer;

Begin

NSUB: = Length (Substring);

PSUB: = PCHAR (SUBSTRING);

PSRC: = PCHAR (Astring);

p: = psrc;

While (n> 0) DO

Begin

P: = Strpos (P, PSUB);

IF (p = nil) THEN

Break;

INC (P, NSUB);

DEC (n);

END;

IF (n = 0) AND (P <> nil) THEN

Result: = P - PSRC

Else

Result: = 0;

END;

Const

Str = 'Function GetnstringPOS (N: Integer; Substring, Astring: String): Integer;';

Substr = 'String';

TEST_COUNT = 5;

Procedure TFORM1.TESTS (Const Funcs: array of tposnfunc;

Const funcnames: array of string;

Istr: integer;

LoopCount: integer;

VAR

I, J, K: Integer;

TM: longword;

Func: tposnfunc;

Count: integer;

RETV: ARRAY OF INTEGER;

Results: array of longword;

Begin

Count: = Length (FUNCS);

Assert (count = length (funcnames));

Memo1.Lines.Add (Format ('Substr Index:% D; Loop Count =% D',

[istr, loopcount]);

SETLENGTH (RETV, COUNT);

SetLENGTH (RESUNT);

For j: = 0 to count-1 do

RESULTS [J]: = 0;

Fork: = 1 to test_count do

Begin

For j: = 0 to count-1 do

Begin

Func: = funcs [j];

TM: = GettickCount;

For i: = 1 to loopcount do

RETV [J]: = FUNC (ISTR, SUBSTR, STR);

INC (Results [J], GettickCount - TM);

END;

END;

For j: = 0 to count-1 dobegin

Memo1.Lines.Add (Format ('% s: return% D; TIMING:% N MS',

[FuncNames [J], RETV [J],

Results [J] * 1.0 / Test_count]))

END;

END;

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

VAR

i: integer;

Begin

For i: = 1 to 4 do

Begin

Memo1.Lines.Add (Format ('% d:', [i]));

Tests ([@getnstringpos], ['getnstringpos'], i, 1000);

Tests ([@POSN_POS, @POSN_POSEX, @POSN_STRPOS],

['POSN_POS', 'POSN_POSEX', 'POSN_STRPOS'],

I, 100000);

END;

END;

Procedure TFORM1.FormCreate (Sender: TOBJECT);

Begin

Memo1.clear;

Memo1.Lines.Add (Format ('Search "% S" for "% s", [str, substr]);

END;

End.

Unit1.dfm:

Object Form1: TFORM1

LEFT = 243

TOP = 164

Width = 578

HEIGHT = 516

Alphablendvalue = 192

CAPTION = 'FORM1'

Color = CLBTNFACE

Font.charset = default_charset

Font.color = CLWINDOWTEXT

Font.height = -11

Font.name = 'MS SANS Serif'

Font.style = []

OldcreateOrder = FALSE

Position = Podefaultposonly

Oncreate = formcreate

DesignSize =

570

489)

Pixelsperinch = 96

TEXTHEIGHT = 13

Object Memo1: TMEMO

LEFT = 3

TOP = 32

Width = 565

HEIGHT = 457

Anchors = [Akleft, Aktop, Akright, Akbottom]

Lines.strings =

'MEMO1')

Scrollbars = SSVertical

Taborder = 0

end

Object Button1: TButton

LEFT = 3

TOP = 6

Width = 75

HEIGHT = 25

CAPTION = 'Button1'

Taborder = 1

Onclick = button1click

end

end

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

New Post(0)