Pointer(s)^ versus s[1]

前端 未结 7 987
南旧
南旧 2021-02-12 14:56

In a function that reads data (data meaning exclusively strings) from disk, which should I prefer? Which is better?

A) DiskStream.Read(Pointer(s         


        
相关标签:
7条回答
  • 2021-02-12 15:22

    If you care about optimization you should prefer the first variant. Just look at the code generated by compiler:

    Unit7.pas.98: Stream.Read(Pointer(S)^, 10);
    00470EA9 8B55FC           mov edx,[ebp-$04]
    00470EAC B90A000000       mov ecx,$0000000a
    00470EB1 8BC6             mov eax,esi
    00470EB3 8B18             mov ebx,[eax]
    00470EB5 FF530C           call dword ptr [ebx+$0c]
    
    Unit7.pas.99: Stream.Read(s[1], 10);
    00470EB8 8B5DFC           mov ebx,[ebp-$04]
    00470EBB 85DB             test ebx,ebx
    00470EBD 7418             jz $00470ed7
    00470EBF 8BC3             mov eax,ebx
    00470EC1 83E80A           sub eax,$0a
    00470EC4 66833802         cmp word ptr [eax],$02
    00470EC8 740D             jz $00470ed7
    00470ECA 8D45FC           lea eax,[ebp-$04]
    00470ECD 8B55FC           mov edx,[ebp-$04]
    00470ED0 E8CB3FF9FF       call @InternalUStrFromLStr
    00470ED5 8BD8             mov ebx,eax
    00470ED7 8D45FC           lea eax,[ebp-$04]
    00470EDA E89950F9FF       call @UniqueStringU
    00470EDF 8BD0             mov edx,eax
    00470EE1 B90A000000       mov ecx,$0000000a
    00470EE6 8BC6             mov eax,esi
    00470EE8 8B18             mov ebx,[eax]
    00470EEA FF530C           call dword ptr [ebx+$0c]
    

    UPDATE

    The above code is generated by Delphi 2009 compiler. You can improve the code by using {$STRINGCHECKS OFF} directive, but you still have UniqueStringU function call overhead:

    Unit7.pas.100: Stream.Read(s[1], 10);
    00470EB8 8D45FC           lea eax,[ebp-$04]
    00470EBB E8B850F9FF       call @UniqueStringU
    00470EC0 8BD0             mov edx,eax
    00470EC2 B90A000000       mov ecx,$0000000a
    00470EC7 8BC3             mov eax,ebx
    00470EC9 8B18             mov ebx,[eax]
    00470ECB FF530C           call dword ptr [ebx+$0c]
    
    0 讨论(0)
提交回复
热议问题