22

所有程序都应该做的一个常见条件是检查字符串是否为空。

采取以下陈述:

(1)

if Length(Str)=0 then
  // do something

(2)

if Str='' then
  // do something
4

3 回答 3

41

在 XE2 中,if str = ''编译为更好的Ansi 和 Unicode 字符串的更快代码。if Length(str) = 0编译为更好的宽字符串的更快代码。

测试程序:

{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N-,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1}
program Project172;

{$APPTYPE CONSOLE}

{$R *.res}

var
  sa1,sa2: AnsiString;
  sw1,sw2: WideString;
  su1,su2: UnicodeString;

begin
  if Length(sa1) = 0 then
    ;
  if sa2 = '' then
    ;
  if Length(sw1) = 0 then
    ;
  if sw2 = '' then
    ;
  if Length(su1) = 0 then
    ;
  if su2 = '' then
    ;
end.

编译代码:

Project172.dpr.14: if Length(sa1) = 0 then
004050E2 A19C9B4000       mov eax,[$00409b9c]
004050E7 85C0             test eax,eax
004050E9 7405             jz $004050f0
004050EB 83E804           sub eax,$04
004050EE 8B00             mov eax,[eax]
004050F0 85C0             test eax,eax

Project172.dpr.16: if sa2 = '' then
004050F2 833DA09B400000   cmp dword ptr [$00409ba0],$00

Project172.dpr.18: if Length(sw1) = 0 then
004050F9 A1A49B4000       mov eax,[$00409ba4]
004050FE 85C0             test eax,eax
00405100 7407             jz $00405109
00405102 83E804           sub eax,$04
00405105 8B00             mov eax,[eax]
00405107 D1E8             shr eax,1
00405109 85C0             test eax,eax

Project172.dpr.20: if sw2 = '' then
0040510B A1A89B4000       mov eax,[$00409ba8]
00405110 33D2             xor edx,edx
00405112 E839E8FFFF       call @WStrEqual

Project172.dpr.22: if Length(su1) = 0 then
00405117 A1AC9B4000       mov eax,[$00409bac]
0040511C 85C0             test eax,eax
0040511E 7405             jz $00405125
00405120 83E804           sub eax,$04
00405123 8B00             mov eax,[eax]
00405125 85C0             test eax,eax

Project172.dpr.24: if su2 = '' then
00405127 833DB09B400000   cmp dword ptr [$00409bb0],$00

如果禁用优化,差异会更大。

Project172.dpr.14: if Length(sa1) = 0 then
004050E2 A19C9B4000       mov eax,[$00409b9c]
004050E7 8945EC           mov [ebp-$14],eax
004050EA 837DEC00         cmp dword ptr [ebp-$14],$00
004050EE 740B             jz $004050fb
004050F0 8B45EC           mov eax,[ebp-$14]
004050F3 83E804           sub eax,$04
004050F6 8B00             mov eax,[eax]
004050F8 8945EC           mov [ebp-$14],eax
004050FB 837DEC00         cmp dword ptr [ebp-$14],$00

Project172.dpr.16: if sa2 = '' then
004050FF 833DA09B400000   cmp dword ptr [$00409ba0],$00

Project172.dpr.18: if Length(sw1) = 0 then
00405106 A1A49B4000       mov eax,[$00409ba4]
0040510B 8945E8           mov [ebp-$18],eax
0040510E 837DE800         cmp dword ptr [ebp-$18],$00
00405112 740D             jz $00405121
00405114 8B45E8           mov eax,[ebp-$18]
00405117 83E804           sub eax,$04
0040511A 8B00             mov eax,[eax]
0040511C D1E8             shr eax,1
0040511E 8945E8           mov [ebp-$18],eax
00405121 837DE800         cmp dword ptr [ebp-$18],$00

Project172.dpr.20: if sw2 = '' then
00405125 A1A89B4000       mov eax,[$00409ba8]
0040512A 33D2             xor edx,edx
0040512C E81FE8FFFF       call @WStrEqual

Project172.dpr.22: if Length(su1) = 0 then
00405131 A1AC9B4000       mov eax,[$00409bac]
00405136 8945E4           mov [ebp-$1c],eax
00405139 837DE400         cmp dword ptr [ebp-$1c],$00
0040513D 740B             jz $0040514a
0040513F 8B45E4           mov eax,[ebp-$1c]
00405142 83E804           sub eax,$04
00405145 8B00             mov eax,[eax]
00405147 8945E4           mov [ebp-$1c],eax
0040514A 837DE400         cmp dword ptr [ebp-$1c],$00

Project172.dpr.24: if su2 = '' then
0040514E 833DB09B400000   cmp dword ptr [$00409bb0],$00
于 2012-05-16T14:07:24.813 回答
24

从语义上讲,它们是相同的,不会有明显的性能差异。因此,我们只能为代码的读者寻找清晰性。

if Str='' then

在我看来是可读的版本。看看你的问题的标题:

检查空字符串的更好方法是什么?

在您的脑海中,您将其视为空字符串,而不是长度为 0 的字符串。因此,请按照与您的观点相匹配的方式编写代码。

于 2012-05-16T13:58:03.003 回答
1

为了扩展@gabr 的答案,如果我们修改源以添加额外的比较EmptyAnsiStr/EmptyWideStr/EmptyStr,我们会看到来自 XE1 的以下反汇编:

Project1.dpr.21: if sa3 = EmptyAnsiStr then
004111DD A1807E4100       mov eax,[$00417e80]
004111E2 8B15EC2C4100     mov edx,[$00412cec]
004111E8 8B12             mov edx,[edx]
004111EA E83148FFFF       call @LStrEqual

Project1.dpr.27: if sw3 = EmptyWideStr then
0041120D A18C7E4100       mov eax,[$00417e8c]
00411212 8B15E82C4100     mov edx,[$00412ce8]
00411218 8B12             mov edx,[edx]
0041121A E8CD49FFFF       call @WStrEqual

Project1.dpr.33: if su3 = EmptyStr then
00411236 A1987E4100       mov eax,[$00417e98]
0041123B 8B15BC2D4100     mov edx,[$00412dbc]
00411241 8B12             mov edx,[edx]
00411243 E8DC4CFFFF       call @UStrEqual

所有 3 个都需要函数调用。

于 2012-05-17T14:03:15.223 回答