Tips for using pointers to efficiently compare strings in C #

Source: Internet
Author: User

Determine whether the random strings are equalProgramIn the C ++ era, we can convert every four bytes in a string into an int object and compare four characters at a time using an int object, to achieve relatively efficient string comparison. So can this idea be implemented in C? The answer is yes.

To use the above idea in C #, two problems must be solved. One is to use pointers in C #, and the locations of managed variables pointed by pointers cannot be reallocated by GC. Second, the correspondence between the hosted string and Int or long in the memory.

ManyArticleThe method for using pointers in C # is described in detail in this article. The method for enabling the unsafe switch is, right-click solution directory -- select Properties -- then select "allow unsafe" in the build dialog boxCodeIn C #, you can use the unsafe keyword to mark the areas where pointers can be used. We know that managed variables are randomly allocated, recycled, and adjusted by the system in the memory. Therefore, pointers to managed variables may point to the wrong region due to random adjustment of managed variables. To ensure that the Pointer Points to the same hosted variable from start to end, the fixed statement is provided in C # to complete the task. The pointer marked with fixed points to the Change volume from beginning to end in the area marked with fixed, and the system does not adjust or assign the managed variable. The Code is as follows:

String STR = "Hello world! ";
Unsafe
{
Fixed (char * PS = Str)
{
// In this region, PS always points to the managed string Str

}
}

Sizeof can be used to determine that long in C # occupies 8 bytes. In the above Code, if we convert the first 8 bytes of PS into long type, what is the result? We use the following code for testing:
Long n = 0;
Long nlow = 0, nhigh = 0;
String STR = "Hello world! ";
Unsafe
{
Fixed (char * PS = Str)
{
Char * pstemp = Ps;

N = * (long *) pstemp;

Nlow = N & 0 xFFFF; // obtain the last two bytes
Nhigh = (n> 16) & 0 xFFFF; // obtains the 3th byte.

MessageBox. Show (char) nlow). tostring () + "" + (char) nhigh). tostring ());

Nlow = (n> 32) & 0 xFFFF; // obtain the 5th and 6th bytes.
Nhigh = (n> 48) & 0 xFFFF; // obtains the 7 th and 8 th bytes.

MessageBox. Show (char) nlow). tostring () + "" + (char) nhigh). tostring ());

}
}
It can be seen that the output in MessageBox above is H, E, L, and l, respectively, which is not the 8 characters we intuitively think. This is because in the conversion process, each character is converted from byte to a two-byte short integer short. Therefore, after the string pointer is strongly converted to long, the value of this long variable can be 4 characters. Therefore, comparing the values of two long objects is actually comparing the values of these four characters, in fact, a comparison operation can compare four characters at the same time.

The code for comparing the two strings is as follows:
Protected bool isequals (string str1, string str2)
{
Bool Bret = true;
Int NC1 = 0, NC2 = 0, nlen = 0;
Int I = 0;
If (str1.length! = Str2.length) // If the length is not equal, the string is not equal.
{
Return false;
}
// If the length is not a multiple of 4, the complement is used.

NC1 = (str1.length % 4 )! = 0 )? (4-str1.length % 4): 0; // calculates the compensation bit.
NC2 = (str2.length % 4 )! = 0 )? (4-str1.length % 4): 0; // calculates the compensation bit.
Nlen = (NC1> NC2 )? NC1: NC2;
For (I = 0; I {
If (I <NC1)
{
Str1 + = "";
}
If (I <NC2)
{
Str2 + = "";
}
}
Unsafe
{
Fixed (char * psstr1 = str1) fixed (char * psstr2 = str2)
{
Char * pstemp1 = psstr1;
Char * pstemp2 = psstr2;

While (I <str1.length)
{
If (* (long *) pstemp1! = (* (Long *) pstemp2) // compare four characters at a time
{
Bret = false;
Break;
}
I + = 4;
Pstemp1 + = 4;
Pstemp2 + = 4;
}
}
}
Return Bret;
}

This article references. NET Framework 3.5Source codeIn the string class private unsafe static bool implements shelper (string stra, string strb) Method

This article is from csdnblog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.