내가 공백으로 구분 된 문자열에 문자열 계산하는 몇 가지 기능을 가지고 있습니다 :델파이 하위 문자열 카운트 성능
program Project2;
{$APPTYPE CONSOLE}
{$R *.res}
uses System.SysUtils,windows;
//s=string to search in
//t=string to search for
//cs=delimiters
function f0(const s,t:string;const cs:tsyscharset):integer;
var
p,q:pchar;
u:string;
begin
result:=0;
p:=pointer(s);
if p<>nil then
while p^<>#0 do
begin
while (p^<>#0) and charinset(p^,cs) do inc(p);
q:=p;
while (p^<>#0) and not charinset(p^,cs) do inc(p);
if p>q then
begin
setstring(u,q,p-q);
//writeln('[',u,']');
if u=t then inc(result);
end;
end;
end;
function f1(const s,t:string;const cs:tsyscharset):integer;
var
i,j,l:integer;
u:string;
begin
result:=0;
l:=length(s);
i:=1;
while i<=l do
begin
while (i<=l) and charinset(s[i],cs) do inc(i);
j:=i;
while (i<=l) and not charinset(s[i],cs) do inc(i);
if i>j then
begin
u:=copy(s,j,i-j);
//writeln('[',u,']');
if u=t then inc(result);
end;
end;
end;
function f2(const s,t:string;const cs:tsyscharset):integer;
var
i,j,l:integer;
u:string;
begin
result:=0;
l:=length(s);
i:=1;
while i<=l do
begin
while (i<=l) and charinset(s[i],cs) do inc(i);
j:=i;
while (i<=l) and not charinset(s[i],cs) do inc(i);
if i>j then
begin
setlength(u,i-j);
move(s[j],pointer(u)^,(i-j)*2);
//writeln('[',u,']');
if u=t then inc(result);
end;
end;
end;
type
tfunc=function(const s,t:string;const cs:tsyscharset):integer;
const
s=' de foo de'+#13+' baz blah de de blah'+#10+' asd de qwe rtz un f'+#9+' t de ds w de ';
t='de';
cs=[' ',#13,#10,#9];//CR,LF,TAB
n=5000000;
procedure time(i:integer;f:tfunc);
var
j,k:integer;
start,finish,freq:int64;
begin
QueryPerformanceCounter(start);
for j := 1 to n do k:=f(s,t,cs);
QueryPerformanceCounter(finish);
QueryPerformanceFrequency(freq);
Writeln(Format('f%u:%u:%.3fs',[i,k,(finish-start)/freq]));
end;
const
funcs:array[0..2] of tfunc=(f0,f1,f2);
var
i:integer;
begin
setpriorityclass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
for i := low(funcs) to high(funcs) do time(i,funcs[i]);
readln
end.
속도 결과
f0:7:7,624s
f1:7:8,066s
f2:7:6,454s
내 첫 번째 질문입니다 것은 :보다 빠른 F2 이유 f0?
두 번째 질문은 다음과 같습니다. 인라인 어셈블러없이이 방법을 훨씬 더 최적화 할 수있는 아이디어가 있습니까?
IDE : Delphi XE2 (유니 코드)
감사합니다. 사용자의 기능을 시험해 보았지만 무한 루프로 실행되는 것 같습니다. 키워드를 두 번째 키워드 (cs의 ord (s [i]))에 추가하지 않으면 충돌이 발생합니다. – tim93
이제 알겠습니다. cs 매개 변수를 사용하여도 내 기능보다 몇 배 빠릅니다. 감사합니다. – tim93