First of all, the conclusion of the test is as follows:
char operates faster than string.
string.Equals() is faster than===.
IndexOf() is much faster than StartWith().
LastIndexOf() is much faster than EndWith().
When manipulating strings, the efficiency will be greatly improved by comparing characters one by one.
Comparison of IndexOf(string) and IndexOf(char):
Test code
using UnityEngine; using System.Diagnostics; /// <summary> /// PerformanceTest /// ZhangYu 2019-07-27 /// <para>blog:https://segmentfault.com/u/bingfengbaidu</para> /// </summary> public class PerformanceTest : MonoBehaviour { public int times = 1; private Stopwatch watch = new Stopwatch(); private void Start () { Test(); } private void Test() { times = 1000000; FindStringTest(); } private void FindStringTest() { string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; IndexOfStringTest(str, ">"); IndexOfCharTest(str, '>'); } private void IndexOfStringTest(string str, string targetStr) { watch.Reset(); watch.Start(); for (int i = 0; i < times; i++) { str.IndexOf(targetStr); } watch.Stop(); print("IndexOf(string):" + watch.ElapsedMilliseconds); } private void IndexOfCharTest(string str, char targetChar) { watch.Reset(); watch.Start(); for (int i = 0; i < times; i++) { str.IndexOf(targetChar); } watch.Stop(); print("IndexOf(char)" + watch.ElapsedMilliseconds); } }
Test results:
IndexOf(string):160ms
IndexOf(char):100ms
Test conclusion:
IndexOf(char) is about 37% faster than IndexOf(string) when searching for a single character.
IndexOf(string) == 0 and StartsWidth(string):
Test code:
using UnityEngine; using System; using System.Diagnostics; /// <summary> /// PerformanceTest /// ZhangYu 2019-07-27 /// <para>blog:https://segmentfault.com/u/bingfengbaidu</para> /// </summary> public class PerformanceTest : MonoBehaviour { public int times = 1; private Stopwatch watch = new Stopwatch(); private void Start () { Test(); } private void Test() { FindTest(); } private void FindTest() { times = 1000000; string str1 = "ABCDEFGHIJKLMNOPQRSTUVWX"; string str2 = "Test"; IndexOfTest1(str1, str2); IndexOfTest2(str1, str2); StartWithTest1(str1, str2); StartWithTest2(str1, str2); } private void IndexOfTest1(string str1, string str2) { watch.Reset(); watch.Start(); for (int i = 0; i < times; i++) { if (str1.IndexOf(str2) == 0); } watch.Stop(); print("str1.IndexOf(str2):" + watch.ElapsedMilliseconds); } private void IndexOfTest2(string str1, string str2) { watch.Reset(); watch.Start(); for (int i = 0; i < times; i++) { if (str1.IndexOf(str2, StringComparison.Ordinal) == 0); } watch.Stop(); print("str1.IndexOf(str2, StringComparison.Ordinal):" + watch.ElapsedMilliseconds); } private void StartWithTest1(string str1, string str2) { watch.Reset(); watch.Start(); for (int i = 0; i < times; i++) { str1.StartsWith(str2); } watch.Stop(); print("str1.StartsWith(str2):" + watch.ElapsedMilliseconds); } private void StartWithTest2(string str1, string str2) { watch.Reset(); watch.Start(); for (int i = 0; i < times; i++) { CharsStartWith(str1, str2); } watch.Stop(); print("CharsStartWith(str1, str2):" + watch.ElapsedMilliseconds); } private bool CharsStartWith(string str1, string str2) { if (str2 == null) return false; if (str1.Length < str2.Length) return false; for (int i = 0; i < str2.Length; i++) { if (str1[i] != str2[i]) return false; } return true; } }
Test results:
str1.IndexOf(str2):153ms
str1.IndexOf(str2, StringComparison.Ordinal):167ms
str1.StartsWith(str2):1024ms
CharsStartWith(str1, str2):35ms
Test conclusion:
The highest efficiency of character-by-character judgment is four times that of IndexOf(string) == 0.
string.StartsWith(string) has the lowest efficiency (there must be redundant operations within this API, otherwise it won't be so slow as IndexOf (string) = 0)
Comparisons between Equals and Compare:
Test code:
using UnityEngine; using System; using System.Diagnostics; /// <summary> /// PerformanceTest /// ZhangYu 2019-07-27 /// <para>blog:https://segmentfault.com/u/bingfengbaidu</para> /// </summary> public class PerformanceTest : MonoBehaviour { public int times = 1; private Stopwatch watch = new Stopwatch(); private void Start () { Test(); } private void Test() { times = 1000000; CompareTest(); } private void CompareTest() { string str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string str2 = "N"; CompareTest1(str1, str2); CompareTest2(str1, str2); CompareTest3(str1, str2); CompareTest4(str1, str2); CompareTest5(str1, str2); } private void CompareTest1(string str1, string str2) { watch.Reset(); watch.Start(); for (int i = 0; i < times; i++) { if (str1 == str2); } watch.Stop(); print("str1 == str2:" + watch.ElapsedMilliseconds); } private void CompareTest2(string str1, string str2) { watch.Reset(); watch.Start(); for (int i = 0; i < times; i++) { str1.Equals(str2); } watch.Stop(); print("str1.Equals(str2):" + watch.ElapsedMilliseconds); } private void CompareTest3(string str1, string str2) { watch.Reset(); watch.Start(); for (int i = 0; i < times; i++) { string.Equals(str1, str2); } watch.Stop(); print("string.Equals(str1, str2):" + watch.ElapsedMilliseconds); } private void CompareTest4(string str1, string str2) { watch.Reset(); watch.Start(); for (int i = 0; i < times; i++) { string.Compare(str1, str2); } watch.Stop(); print("string.Compare(str1, str2):" + watch.ElapsedMilliseconds); } private void CompareTest5(string str1, string str2) { watch.Reset(); watch.Start(); StringComparison ordinal = StringComparison.Ordinal; for (int i = 0; i < times; i++) { string.Compare(str1, str2, ordinal); } watch.Stop(); print("string.Compare(str1, str2, ordinal):" + watch.ElapsedMilliseconds); } }
Test results:
str1 == str2:26ms
str1.Equals(str2):26ms
string.Equals(str1, str2):20ms
string.Compare(str1, str2):1028ms
string.Compare(str1, str2, ordinal):48ms
Test conclusion:
When comparing strings, string.Equals(str1,str2) is the fastest, and str1==str1 and str1.Equals(str2) have the same efficiency.
EndWith and character-by-character judgment comparison:
Test code:
using UnityEngine; using System.Diagnostics; /// <summary> /// PerformanceTest /// ZhangYu 2019-07-27 /// <para>blog:https://segmentfault.com/u/bingfengbaidu</para> /// </summary> public class PerformanceTest : MonoBehaviour { public int times = 1; private Stopwatch watch = new Stopwatch(); private void Start () { Test(); } private void Test() { times = 1000000; EndFindTest(); } private void EndFindTest() { string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; EndFindTest1(str, "[]"); EndFindTest2(str, "[]"); } private void EndFindTest1(string str1, string str2) { watch.Reset(); watch.Start(); for (int i = 0; i < times; i++) { str1.EndsWith(str2); } watch.Stop(); print("str1.EndsWith(str2):" + watch.ElapsedMilliseconds); } private void EndFindTest2(string str1, string str2) { watch.Reset(); watch.Start(); for (int i = 0; i < times; i++) { CharsEndWith(str1, str2); } watch.Stop(); print("CharsEndWith(str1, str2):" + watch.ElapsedMilliseconds); } // Character by character to determine whether str1 ends with str2 private bool CharsEndWith(string str1, string str2) { if (str2 == null) return false; if (str1.Length < str2.Length) return false; for (int i = 1; i <= str2.Length; i++) { if (str1[str1.Length - i] != str2[str2.Length - i]) return false; } return true; } }
Test results:
str1.EndsWith(str2):28480ms
CharsEndWith(str1, str2):36ms
Test conclusion:
Character by character judgment is much faster than EndWith(string).