Iva*_*nov 9 delphi optimization performance
我已经看到有关SO的评论在声明中说" <>比快="或" !=快于==" if().
我想知道为什么会这样.你能在asm中展示一个例子吗?
谢谢!:)
编辑:
这就是他所做的.
function Check(var MemoryData:Array of byte;MemorySignature:Array of byte;Position:integer):boolean;
var i:byte;
begin
Result := True; //moved at top. Your function always returned 'True'. This is what you wanted?
for i := 0 to Length(MemorySignature) - 1 do //are you sure??? Perhaps you want High(MemorySignature) here...
begin
{!} if MemorySignature[i] <> $FF then //speedup - '<>' evaluates faster than '='
begin
Result:=memorydata[i + position] <> MemorySignature[i]; //speedup.
if not Result then
Break; //added this! - speedup. We already know the result. So, no need to scan till end.
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
我认为您之前的问题中存在一些混淆,即您试图实施的算法是什么,因此声称"加速"声称要做什么.
这是Delphi 2007的一些反汇编.优化.(注意,优化关闭稍微改变了代码,但不是以相关的方式.
Unit70.pas.31: for I := 0 to 100 do
004552B5 33C0 xor eax,eax
Unit70.pas.33: if i = j then
004552B7 3B02 cmp eax,[edx]
004552B9 7506 jnz $004552c1
Unit70.pas.34: k := k+1;
004552BB FF05D0DC4500 inc dword ptr [$0045dcd0]
Unit70.pas.35: if i <> j then
004552C1 3B02 cmp eax,[edx]
004552C3 7406 jz $004552cb
Unit70.pas.36: l := l + 1;
004552C5 FF05D4DC4500 inc dword ptr [$0045dcd4]
Unit70.pas.37: end;
004552CB 40 inc eax
Unit70.pas.31: for I := 0 to 100 do
004552CC 83F865 cmp eax,$65
004552CF 75E6 jnz $004552b7
Unit70.pas.38: end;
004552D1 C3 ret
Run Code Online (Sandbox Code Playgroud)
如您所见,两种情况之间的唯一区别是jz与jnz指令.这些将以相同的速度运行.可能更多地影响事物的是分支被占用的频率,以及整个循环是否适合缓存.
如果你从string.op_Equality和string.op_Inequality方法中查看IL ,你会看到两个内部调用string.Equals.
但是op_Inequality反转了结果.这是两个IL声明.
我会说它们的性能是相同的,对于==语句可能有一个小的(非常小的,非常小的)更好的性能.但我相信优化器和JIT编译器会删除它.
| 归档时间: |
|
| 查看次数: |
1803 次 |
| 最近记录: |