大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> Script -> 在串中查找第i个子串的位置及效率评测

在串中查找第i个子串的位置及效率评测

时间: 2021-07-31 作者:daque

步调附在结果,这边是少许证明笔墨:1、为赶快写好,没有运用该当运用的遏制台办法,而是运用了gui办法;2、尝试的样例是搜索包括有到处子串的字符串,四次辨别查四个场所。这个在button1click本领中实行,它挪用tests来举行简直尝试,以被测因变量、第几次展示、轮回度数为参数;3、tests顺序在一个轮回中反复挪用每个简直的因变量,同声为了公道起见(大概前方的因变量为反面的铺了少许路——外存、高速缓冲),如许的尝试举行test_count次,结果输入历次的平衡功夫;4、过程前期的尝试,lw549 的代码简直功效不高,以是独立给它小少许的轮回度数(一千次),免得形成步调佯死局面;其它的为十万次;5、其它三个因变量的思维为:posn_pos: 运用pos因变量及copy因变量;posn_posex: 运用delphi 7中减少的posex因变量;posn_strpos: 运用strpos因变量。步调输入:search "function getnsubstringpos(n: integer; substring,astring: string): integer;" for "string"1:substr index: 1; loop count = 1000getnsubstringpos: return 17; timing: 37.60 mssubstr index: 1; loop count = 100000posn_pos: return 17; timing: 40.40 msposn_posex: return 17; timing: 15.60 msposn_strpos: return 22; timing: 37.80 ms2:substr index: 2; loop count = 1000getnsubstringpos: return 42; timing: 96.80 mssubstr index: 2; loop count = 100000posn_pos: return 42; timing: 81.20 msposn_posex: return 42; timing: 47.00 msposn_strpos: return 47; timing: 53.00 ms3:substr index: 3; loop count = 1000getnsubstringpos: return 50; timing: 109.40 mssubstr index: 3; loop count = 100000posn_pos: return 50; timing: 118.80 msposn_posex: return 50; timing: 53.00 msposn_strpos: return 55; timing: 62.60 ms4:substr index: 4; loop count = 1000getnsubstringpos: return 58; timing: 128.20 mssubstr index: 4; loop count = 100000posn_pos: return 58; timing: 162.60 msposn_posex: return 58; timing: 59.40 msposn_strpos: return 63; timing: 74.80 ms

不妨看出,尝试的截止(功效)是:  posn_posex > posn_strpos > posn_pos >> getnsubstringpos 。我从来憧憬的是 posn_strpos 最利害,但截止不是。估量是 posex 优化得比拟利害。附代码:

unit1.pas:

unit unit1;interfaceuses 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;implementationuses strutils;{$r *.dfm}function getnsubstringpos(n: integer; substring,astring: string): integer;//归来第n个substring在astring中展示的场所//即使没找到,归来-1var 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) < length(substring) + pos then exit;//未找到 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 getnsubstringpos(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( results, count ); for j:=0 to count-1 do results[j] := 0; for k:=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 do begin 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( [@getnsubstringpos], ['getnsubstringpos'], 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 endend

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.