After I wrote a no-worth article, after further study, I added this part and I understand why the last result will appear. The last gray font is the previous article, there is a mistake, for reference only, everyone can only look at the black font. After writing the previous article, I have to accept criticism. Obviously, I didn't read the book "Applied Microsoft .Net Framework Programming", otherwise it will not be tested. Moreover, the test method is also written. Afterwards, it understands the truth and essence. Reflection is as follows: After the anti-compilation, "==" in .NET is implemented in this:
Public Static Bool Operator == (String A, String B)
{
Return string.equals (a, b);
}
The implementation of strings.equals (string, string) is as follows:
Public Static Bool Equals (String A, String B)
{
IF (a == b)
{
Return True;
}
IF ((a! = null) && (b! = null))
{
Return a.equals (b);
}
Return False;
}
Final String.Equals (String) implementation (through internal calls, how to implement it is unknown):
[MethodImpl (MethodImploptions.InternalCall)]
Public Extern Bool Equals (String Value);
It can be seen that == first compare references for two objects, if the reference directly returns TRUE. A. Equals (b) is called if the reference is different. So, in the above program, all "67412" is the same object, ==, it is more step by step than Equals, and does not enter String.Equals (String). Returns True. When compared with "67413", the reference is different, == By calling string.equals (string), it is returned, so it has used a more reference time. If the two strings are referenced by different contents, then the type of test that returns FALSE above the time should be the same. If you only know == is implemented by equals, now you should know why the first group == is fast than Equals.
Below is the result of the anti-assessment of several statements, you can also see that all "67412" are from the same address: string tobetested = "67412"; 00000000 Push EBP 00000003 SUB ESP, 8 00000006 PUSH EDI 00000007 push esi 00000008 push ebx 00000009 xor esi, esi 0000000b xor edi, edi 0000000d mov eax, dword ptr ds: [01AE1058h] 00000013 mov esi, eax toBeTested.Equals ( "67412"); 00000015 mov edx, dword ptr ds: [ 01AE1058h] 0000001b mov ecx, esi 0000001d cmp dword ptr [ecx], ecx 0000001f call dword ptr ds: [79C126F4h] 00000025 nop result = toBeTested == "67412"; 00000026 mov edx, dword ptr ds: [01AE1058h] 0000002c mov ecx , esi 0000002e call dword ptr ds: [79C126FCh] 00000034 movzx ebx, al 00000037 movzx eax, bl 0000003a mov edi, eax result = toBeTested.Equals ( "67413"); 0000003c mov edx, dword ptr ds: [01AE105Ch] 00000042 mov ECX, ESI 00000044 CMP dword ptr [ecx], ecx 00000046 call dword ptr ds: [79C126F4h] 0000004c movzx ebx, al 0000004f movzx eax, bl 00000052 mov edi, eax result = toBeTested == "67413"; 00000054 mov edx, dword ptr ds: [01AE105Ch ] 0000005a mov ecx, esi 0000005c call dword ptr ds: [79C126FCh] 00000062 movzx ebx, al 00000065 movzx eax, bl 00000068 mov edi, eax} 0000006a nop 0000006b pop ebx 0000006c pop esi 0000006d pop edi 0000006e mov esp, ebp 00000070 pop ebp 00000071 RET
This is completely enough. True big white! There is still a point that is the concept of "character string" mentioned by a netizen. I still don't know much, I don't dare to say it. I read "Applied Microsoft .NET Framework Program" went to ... and I also have not read this book and want to know the comrades of .NET principle recommend it. Attachment: The content of the previous article should compare whether the two strings are equal, there are two ways: string tobetested = "67412"; BOOL RESULT; Result = TOBETESTED.EQUALS ("67413"); and result = tobetested == " 67413 "; Which one is good? Test procedure: int Times = 100000000; int Start, end; int i; bool result; string tobetested = "67412"; start = system.environment.tickcount; for (i = 0; i Start = system.environment; for (i = 0; i Start = system.environment; for (i = 0; i