Further to a previous post of mine, I have done some benchmarking to compare the different alternatives. I am using some old benchmark code from Barry Kelly and added a few more options (word of char in, Char.IsInArray and if then).
Here are the 6 ways compared:
while not CharInSet(cp^, [#0, #13, #10]) do
case statement
while not (cp^ in [#0, #13, #10]) do (raises a warning)
while not (Word(cp^) in [$0, $A, $D]) do
while not (cp^.IsInArray([#0, #13, #10])) do
while (cp^<>#0) and (cp^<>#$A) and (cp^<>#$D) do
Results with optimization turned on.
Win32
1. CharInSet: 0.037 seconds
2. case stmt: 0.039 seconds
3. set test: 0.036 seconds
4. Word set test: 0.037 seconds
5. InArray test: 0.219 seconds
6. if and test: 0.037 seconds
Win64
1. CharInSet: 0.106 seconds
2. case stmt: 0.047 seconds
3. set test: 0.042 seconds
4. Word set test: 0.044 seconds
5. InArray test: 0.248 seconds
6. if and test: 0.036 seconds
What is worrying is that in 64 bits both CharInSet and IsInArray (the recommended options) are much slower than the alternatives. CharInSet is an inlined version of Option 3, but the code cannot take advantage of the specific contents of the set. Here is the assembly code:
CharInSet.dpr.15: while not CharInSet(cp^, [#0, #13, #10]) do
0000000000429DAA 480FB708 movzx rcx,word ptr [rax]
0000000000429DAE 6681F9FF00 cmp cx,$00ff
0000000000429DB3 7711 jnbe P1 + $36
0000000000429DB5 480FB7C9 movzx rcx,cx
0000000000429DB9 480FA30D37000000 bt [rel $00000037],rcx
0000000000429DC1 0F92C1 setb cl
0000000000429DC4 EB02 jmp P1 + $38
0000000000429DC6 33C9 xor ecx,ecx
0000000000429DC8 84C9 test cl,cl
0000000000429DCA 74DA jz P1 + $16
CharInSet.dpr.41: while not (cp^ in [#0, #13, #10]) do
000000000042A05A 480FB708 movzx rcx,word ptr [rax]
000000000042A05E 6683F90F cmp cx,$0f
000000000042A062 7716 jnbe P3 + $3A
000000000042A064 66BA0100 mov dx,$0001
000000000042A068 D3E2 shl edx,cl
000000000042A06A 480FB70D3A000000 movzx rcx,word ptr [rel $0000003a]
000000000042A072 6685CA test dx,cx
000000000042A075 0F95C1 setnz cl
000000000042A078 EB02 jmp P3 + $3C
000000000042A07A 33C9 xor ecx,ecx
000000000042A07C 84C9 test cl,cl
000000000042A07E 74D6 jz P3 + $16
CharInSet.dpr.51: while not (Word(cp^) in [$0, $A, $D]) do
000000000042A1AA 480FB708 movzx rcx,word ptr [rax]
000000000042A1AE 6683F90F cmp cx,$0f
000000000042A1B2 7716 jnbe P4 + $3A
000000000042A1B4 66BA0100 mov dx,$0001
000000000042A1B8 D3E2 shl edx,cl
000000000042A1BA 480FB70D3A000000 movzx rcx,word ptr [rel $0000003a]
000000000042A1C2 6685CA test dx,cx
000000000042A1C5 0F95C1 setnz cl
000000000042A1C8 EB02 jmp P4 + $3C
000000000042A1CA 33C9 xor ecx,ecx
000000000042A1CC 84C9 test cl,cl
000000000042A1CE 74D6 jz P4 + $16
Options 2 and 6 are too verbose and Options 1 and 5 too slow. Option 4 (Word(cp^) in [$0, $A, $D]) produces the same code as option 3 (cp^ in [#0, #13, #10]) without raising an exception and it sounds like a good choice. Any downsides?
Incidentally combining any of the above options with an initial test for the most common case e.g.
while (Word(cp^) > $D) or not (Word(cp^) in [$0, $A, $D]) do
is much faster!
Source code attached.
CharInSet.dpr