zw.delphi不同版本程序运行速度测试

时间:2021-01-13 17:43:19
{
zw.delphi不同版本程序运行速度测试

delphi无论是开发,编译,还是运行,速度方面向来不差,笔者很少进行这种微粒度的优化,调试、
最近,因为项目需要,发现:同一个函数模块,差不多同样的代码,升级后,比2007慢2-3倍
涉及字符串、thashedlist,起初,感觉是uncode字符串函数,替换qstring函数的问题。
测试后,发现:
在短字符串方面,thashedstringlist,甚至字符串退换方面,xe10优化的更好,比d2007速度快20-50%,
真正的瓶颈,出现在xe10对于长字符串(>500k)的处理上,特别是lowercase等函数,xe10版本比d2007慢3倍。
----------------------------------------------

为公平起见,特意采用纯pascal编写相关的测试函数。
理论上,适用于d7和其他版本的delphi,已在d2007,xe10测试通过
测试环境:win7+64位,笔记本:i7-3720QM,8G内存

测试结果如下:
tst001,@d2007,tn,6708 ms,@xe.10,tn,3962 ms,重点测试thashedstringlist,xe10比d2007快了一倍
tst002,@d2007,tn,4227 ms,@xe.10,tn,2231 ms,测试短字符串替换(<500)
tst003,@d2007,tn,6100 ms,@xe.10,tn,4290 ms,测试长字符串替换(>500k)

tst004,测试其他字符串函数,长字符串(>500k)

lowercase,@d2007,tn,296 ms,@xe.10,tn,1092 ms,!!!瓶颈
leftStr,@d2007,tn,281 ms,@xe.10,tn,390 ms
rightStr,@d2007,tn,281 ms,@xe.10,tn,390 ms
pos,@d2007,tn,156 ms,@xe.10,tn,203 ms

}

//---------测试代码

function tst_Str1():string;
var i,j,k1,t0,t2,tn:integer;
s2,kss,dss,ess,vss:string;
xlib:thashedstringlist;
begin //重点测试thashedstringlist
xlib:=thashedstringlist.create;
with mwin do begin msg.lines.add('');t0:=gettickcount;
for i:=0 to 5000 do begin kss:='xk'+intToStr(i+10000);
dss:='dx'+intToStr(i+10000);
xlib.Values[kss]:=dss;
end;
t2:=gettickcount;
//---tst
for i:=1 to 10 do
for j:=1 to 1000 do begin
kss:='xk'+intToStr(j+10000);
s2:=xlib.Values[kss];s2:=dss+s2;
end;
t2:=gettickcount;tn:=t2-t0;
s2:='tst001,tn,'+intToStr(tn)+' ms';msg.lines.add(s2);
end;
xlib.free;
end;


function tst_Str2():string;
var i,j,k1,t0,t2,tn:integer;
s2,dss,ess,vss:string;
begin //测试短字符串替换(<500)
with mwin do begin msg.lines.add('');t0:=gettickcount;
vss:='abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for i:=1 to 500 do
for j:=1 to 5000 do begin
dss:=StringReplace(vss,'efg','123',[rfReplaceAll,rfIgnoreCase]);
end;
t2:=gettickcount;tn:=t2-t0;s2:='tst002,tn,'+intToStr(tn)+' ms';msg.lines.add(s2);
end;
end;

function tst_Str3():string;
var i,j,k1,t0,t2,tn:integer;
s2,dss,ess,vss:string;
begin //测试长字符串替换(>500k)
with mwin do begin msg.lines.add('');
s2:='abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for i:=1 to 3000 do vss:=vss+','+s2;
t0:=gettickcount;
for i:=1 to 2 do
for j:=1 to 5 do begin
dss:=StringReplace(vss,'efg','123',[rfReplaceAll,rfIgnoreCase]);
end;
t2:=gettickcount;tn:=t2-t0;s2:='tst003,tn,'+intToStr(tn)+' ms';msg.lines.add(s2);
end;
end;


function tst_Str4():string;
var i,j,k1,t0,t2,tn:integer;
s2,dss,ess,vss:string;
begin //测试长字符串函数(>500k)
with mwin do begin msg.lines.add('');
s2:='abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for i:=1 to 3000 do vss:=vss+','+s2;
vss:=' '+vss+' ';
//--
t0:=gettickcount;for i:=1 to 10 do for j:=1 to 500 do dss:=lowercase(vss);
t2:=gettickcount;tn:=t2-t0;s2:='tst004,lowercase,tn,'+intToStr(tn)+' ms';msg.lines.add(s2);
//---
t0:=gettickcount;for i:=1 to 2000 do for j:=1 to 5000 do s2:=leftStr(vss,100);
t2:=gettickcount;tn:=t2-t0;s2:='tst004,leftStr,tn,'+intToStr(tn)+' ms';msg.lines.add(s2);
//--
t0:=gettickcount;for i:=1 to 2000 do for j:=1 to 5000 do s2:=rightStr(vss,100);
t2:=gettickcount;tn:=t2-t0;s2:='tst004,rightStr,tn,'+intToStr(tn)+' ms';msg.lines.add(s2);
//--
t0:=gettickcount;for i:=1 to 5000 do for j:=1 to 5000 do k1:=Pos('efg',vss);
t2:=gettickcount;tn:=t2-t0;s2:='tst004,pos,tn,'+intToStr(tn)+' ms';msg.lines.add(s2);
end;
end;