L
LiChaoHui
Unregistered / Unconfirmed
GUEST, unregistred user!
也不知道楼上是怎么测试的,我用了更高精度的测试
下面是多次测试的结果和源代码
计算机配置: PII350 RAM256
测试结果:
第一次
Time set ops: 2665771
Time compare: 2003729
第二次
Time set ops: 2687311
Time compare: 2019305
第三次
Time set ops: 2680279
Time compare: 2025879
第四次
Time set ops: 2704012
Time compare: 2020918
源代码
procedure TForm1.Button1Click(Sender: TObject);
var
A, I: Integer;
//dwStart, dwEnd: DWORD;
t1, t2: Int64;
begin
A := 20;
//dwStart := GetTickCount;
QueryPerformanceCounter(t1);
for I := 0 to 100000000 do
if A in [9, 10, 13, 32] then
A := 20;
//dwEnd := GetTickCount;
QueryPerformanceCounter(t2);
//Memo1.Lines.Add(Format('Time: %d', [dwStart - dwEnd]));
Memo1.Lines.Add(Format('Time set ops: %d', [t2 - t1]));
//dwStart := GetTickCount;
QueryPerformanceCounter(t1);
for I := 0 to 100000000 do
if (A = 9) or (A = 10) or (A = 13) or (A = 32) then
A := 20;
//dwEnd := GetTickCount;
QueryPerformanceCounter(t2);
//Memo1.Lines.Add(Format('Time: %d', [dwStart - dwEnd]));
Memo1.Lines.Add(Format('Time compare: %d', [t2 - t1]));
Memo1.Lines.Add('');
end;
可以得出结论在没有命中时,常规比较比集合运算快 25%以上
而不是快一点点
但是,当集合的元素为8个时,都不命中,常规的比较简直慢了一倍
如果,集合元素为8个,第一个就命中,常规的比较简直快了一倍
情况比较复杂,看那位高手能总结出一个推论,
以作为编程时提升算法性能的参考!!
下面是多次测试的结果和源代码
计算机配置: PII350 RAM256
测试结果:
第一次
Time set ops: 2665771
Time compare: 2003729
第二次
Time set ops: 2687311
Time compare: 2019305
第三次
Time set ops: 2680279
Time compare: 2025879
第四次
Time set ops: 2704012
Time compare: 2020918
源代码
procedure TForm1.Button1Click(Sender: TObject);
var
A, I: Integer;
//dwStart, dwEnd: DWORD;
t1, t2: Int64;
begin
A := 20;
//dwStart := GetTickCount;
QueryPerformanceCounter(t1);
for I := 0 to 100000000 do
if A in [9, 10, 13, 32] then
A := 20;
//dwEnd := GetTickCount;
QueryPerformanceCounter(t2);
//Memo1.Lines.Add(Format('Time: %d', [dwStart - dwEnd]));
Memo1.Lines.Add(Format('Time set ops: %d', [t2 - t1]));
//dwStart := GetTickCount;
QueryPerformanceCounter(t1);
for I := 0 to 100000000 do
if (A = 9) or (A = 10) or (A = 13) or (A = 32) then
A := 20;
//dwEnd := GetTickCount;
QueryPerformanceCounter(t2);
//Memo1.Lines.Add(Format('Time: %d', [dwStart - dwEnd]));
Memo1.Lines.Add(Format('Time compare: %d', [t2 - t1]));
Memo1.Lines.Add('');
end;
可以得出结论在没有命中时,常规比较比集合运算快 25%以上
而不是快一点点
但是,当集合的元素为8个时,都不命中,常规的比较简直慢了一倍
如果,集合元素为8个,第一个就命中,常规的比较简直快了一倍
情况比较复杂,看那位高手能总结出一个推论,
以作为编程时提升算法性能的参考!!