I am afraid that "+" and "StringBuilder" are well known for the string efficiency. I will not go into detail in this article. For this article, please answer the following questions first (assuming that they are based on multiple cyclic and repeated calls ):
1. Which of the Insert and Format methods is more efficient?
2. Who is more efficient in Contains (value) and IndexOf (value?
If you are not interested in this question or are familiar with it, ignore this article. In addition, this article will not explain the actual use of the code in this article.
<1> first, let's take a look at the following scenarios:
str1 = str2 = = .Format(= str1.Insert(, str2);
WriteTime( title,
Add another method to perform loop operations on strings
LoopCalc(Action<, >[] array = [ ( i = ; i < array.Length; i++= i.ToString()= ( i = ; i < array.Length; i++
Add the Insert and Format Efficiency Comparison code for string and call it in the Main method
inserTime = LoopCalc((x, y) => x.Insert( formatTime = LoopCalc((x, y) => .Format(
The running result is as follows:
It is obvious that Insert is more efficient, but this result has limitations. If the string is very long, they will be less efficient after my tests.
Note: I only use Format for String concatenation. The higher efficiency is still StringBuilder. Of course, there are too many irreplaceable functions of Format. Insert and StringBuilder cannot replace it at all, it won't be long here.
<2> still the first
str1 = str2 = (str1.IndexOf(str2) > -) { }
Here we still use the loopc1c method, add the following method, and then call its
indexOfTime = LoopCalc((x, y) => { (x.IndexOf(y) >= containersTime = LoopCalc((x, y) => {
Result
Obviously, Contains are more efficient. Why? I didn't know why before. Now let's look at the source code of the String class (about. NET built-in Class Library source code can be searched by Google, I forgot the address), a lot of code, I will post the following methods in the string class for you to see
CultureInfo.CurrentCulture.CompareInfo.IndexOf( IndexOf(String value, CultureInfo.CurrentCulture.CompareInfo.IndexOf(
Contains( ( IndexOf(value, StringComparison.Ordinal) >=
The following is an overload of IndexOf called by Contains:
IndexOf(value, , IndexOf(String value, IndexOf(value, startIndex, .Length - IndexOf(String value, startIndex, (value == ArgumentNullException( (startIndex < || startIndex > ArgumentOutOfRangeException(, Environment.GetResourceString( (count < || startIndex > .Length - ArgumentOutOfRangeException(,Environment.GetResourceString( CultureInfo.CurrentCulture.CompareInfo.IndexOf( CultureInfo.CurrentCulture.CompareInfo.IndexOf( CultureInfo.InvariantCulture.CompareInfo.IndexOf( CultureInfo.InvariantCulture.CompareInfo.IndexOf( CultureInfo.InvariantCulture.CompareInfo.IndexOf( TextInfo.IndexOfStringOrdinalIgnoreCase( ArgumentException(Environment.GetResourceString(),
The conclusion is similar, but another class is involved here. invariantCulture. compareInfo. I have also read this class of code, which contains the unsafe code, which is not in the scope of this article. However, I have concluded that when I change the IndexOf code in my Demo to "x. indexOf (y, StringComparison. ordinal) ", then the phase difference between the two will be no two.
String itself is the encapsulation of the char Array, which more or less reflects some of the characteristics of the Array, then let's take a look at the Contains and IndexOf in the List set.
<3> the IndexOf method of List does not have the StringComparison enumeration method as the parameter. Go to the code and click LoopCalcList (Action <List <>,> <> [] array = List <> [(I =; I <array. length; I ++ = List <>, = (I =; I <array. length; I ++ indexOfTime = LoopCalcList (x, y) => {(x. indexOf (y)> = containersTime = LoopCalcList (x, y) => {Efficiency Comparison in List
Running result
It is clear that we should unswervingly use Contains when determining whether or not to include them.
(Object) item = (I =; I <_ size; I ++ (Object) _ items [I] = <T> c = EqualityComparer <T> (I =; I <_ size; I ++ (c. equals (_ items [I], item ))Contains method of List class
Array. indexOf (_ items, item,-IndexOf (T item, (index> Array. indexOf (_ items, item, index, _ size-IndexOf (T item, index, (index> (count <| index> _ size-IndexOf method of List class
Summary:
1. This efficiency problem may not matter to some people, but I think it is more important to develop coding habits.
2. use Contains whenever possible (I found many colleagues in the code I changed directly use "IndexOf (value)"). Of course, there will be special exception scenarios.
3. For Insert, I have compiled two extension methods as follows (although the method is simple, it brings more elegance to the Code)
InsertLast( source, InsertFirst( source, source.Insert(