Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.
2011-08-02
In a simple loop, I compare ten million short strings with other strings. 1.4 million of them are identical. Depending on how the comparison is done, the execution speed differs. When the compare operator is used, the ten million tests are done in 0.31 seconds on my laptop.
If Strings(A1) = Strings(A2) Then
When the String.Compare function is used, the ten million tests are done in somewhere between 3.35 to 3.39 seconds. For me, case sensitive tests tend to be slightly slower.
If String.Compare(Strings(A1), Strings(A2)) = 0 Then
The reason that the equals operator is faster, is because it only compares the addresses where the strings are located, and since strings are unique in .NET, two strings with the same value, are stored in the same address. The Compare function looks at the characters in the string, and is therefore slower.
This is the disassembly for the test with the equals operator:
0000018f mov eax,dword ptr [ebp-50h] 00000192 mov edx,dword ptr [ebp-48h] 00000195 cmp eax,dword ptr [edx+4] 00000198 jb 0000019F 0000019a call 67E075C0 0000019f mov eax,dword ptr [edx+eax*4+0Ch] 000001a3 mov dword ptr [ebp+FFFFFF70h],eax 000001a9 mov eax,dword ptr [ebp-54h] 000001ac mov edx,dword ptr [ebp-48h] 000001af cmp eax,dword ptr [edx+4] 000001b2 jb 000001B9 000001b4 call 67E075C0 000001b9 mov eax,dword ptr [edx+eax*4+0Ch] 000001bd mov dword ptr [ebp+FFFFFF6Ch],eax 000001c3 push 0 000001c5 mov ecx,dword ptr [ebp+FFFFFF70h] 000001cb mov edx,dword ptr [ebp+FFFFFF6Ch] 000001d1 call 558156C0 000001d6 mov dword ptr [ebp-74h],eax 000001d9 cmp dword ptr [ebp-74h],0 000001dd sete al 000001e0 movzx eax,al 000001e3 mov dword ptr [ebp-64h],eax 000001e6 cmp dword ptr [ebp-64h],0 000001ea je 00000201
This is the disassembly for the test with the String.Compare function:
0000018f mov eax,dword ptr [ebp-50h] 00000192 mov edx,dword ptr [ebp-48h] 00000195 cmp eax,dword ptr [edx+4] 00000198 jb 0000019F 0000019a call 67F275C0 0000019f mov ecx,dword ptr [edx+eax*4+0Ch] 000001a3 mov eax,dword ptr [ebp-54h] 000001a6 mov edx,dword ptr [ebp-48h] 000001a9 cmp eax,dword ptr [edx+4] 000001ac jb 000001B3 000001ae call 67F275C0 000001b3 mov edx,dword ptr [edx+eax*4+0Ch] 000001b7 call 5E514300 000001bc mov dword ptr [ebp-74h],eax 000001bf cmp dword ptr [ebp-74h],0 000001c3 sete al 000001c6 movzx eax,al 000001c9 mov dword ptr [ebp-64h],eax 000001cc cmp dword ptr [ebp-64h],0 000001d0 je 000001E7
It looks less, but it does more. The actual comparing is not shown, but it checks character until a mismatch is found. Since the difference is big, it’s important to pick the right method. If you add the compile option to compare text as text, the equal operator is almost just as slow as the String.Compare function, so the impact for this option…
Option Compare Text
…is huge.
Categories: Microsoft .NET
Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!
Leave a Reply