.net中的字符串操作性能问题由来已久,幸运的是微软推出了span
一、String.Index 测试
1 var t1 = StopWacherHelper.DoTimer(() => 2 { 3 for (int i = 0; i < count; i++) 4 { 5 str.IndexOf(subStr); 6 } 7 }); 8 9 Console.WriteLine($"string.IndexOf {count} 次用时:{t1} 毫秒");
1 var t2 = StopWacherHelper.DoTimer(() => 2 { 3 var strSpan = str.AsSpan(); 4 5 var subStrSpan = subStr.AsSpan(); 6 7 for (int i = 0; i < count; i++) 8 { 9 strSpan.IndexOf(subStrSpan); 10 } 11 }); 12 13 Console.WriteLine($"Span.IndexOf {count} 次用时:{t2} 毫秒 ");
二、String.Substring 测试
1 var t1 = StopWacherHelper.DoTimer(() => 2 { 3 for (int i = 0; i < count; i++) 4 { 5 str.Substring(0, str.IndexOf(subStr)); 6 } 7 }); 8 9 Console.WriteLine($"String.Substring {count} 次用时:{t1} 毫秒");
1 var t2 = StopWacherHelper.DoTimer(() => 2 { 3 var strSpan = str.AsSpan(); 4 5 var subStrSpan = subStr.AsSpan(); 6 7 var index = strSpan.IndexOf(subStrSpan); 8 9 for (int i = 0; i < count; i++) 10 { 11 strSpan.Slice(0, strSpan.IndexOf(subStrSpan)); 12 } 13 });
三、String.Split 测试
1 var t1 = StopWacherHelper.DoTimer(() => 2 { 3 for (int i = 0; i < count; i++) 4 { 5 var arr = str.Split(new string[] { splitStr }, StringSplitOptions.None); 6 } 7 }); 8 9 Console.WriteLine($"String.Split {count} 次用时:{t1} 毫秒");
1 var t2 = StopWacherHelper.DoTimer(() => 2 { 3 var strSpan = str.AsSpan(); 4 5 var splitSapn = splitStr.AsSpan(); 6 7 int m = 0, n = 0; 8 9 for (int i = 0; i < count; i++) 10 { 11 List<string> arr = new List<string>(); 12 13 while (true) 14 { 15 m = n; 16 n = strSpan.IndexOf(splitSapn); 17 if (n > -1) 18 { 19 arr.Add(strSpan.Slice(0, n).ToString()); 20 strSpan = strSpan.Slice(n + splitSapn.Length); 21 } 22 else 23 { 24 break; 25 } 26 } 27 28 } 29 }); 30 31 Console.WriteLine($"Span.Split {count} 次用时:{t2} 毫秒 ");
四、String.Replace 测试
1 var t1 = StopWacherHelper.DoTimer(() => 2 { 3 for (int i = 0; i < count; i++) 4 { 5 str.Replace(splitStr, replaceStr); 6 } 7 }); 8 9 Console.WriteLine($"String.Replace {count} 次用时:{t1} 毫秒");
1 var t2 = StopWacherHelper.DoTimer(() => 2 { 3 var strSpan = str.AsSpan(); 4 5 var splitSapn = splitStr.AsSpan(); 6 7 int m = 0, n = 0; 8 9 for (int i = 0; i < count; i++) 10 { 11 List<string> arr = new List<string>(); 12 13 while (true) 14 { 15 m = n; 16 n = strSpan.IndexOf(splitSapn); 17 if (n > -1) 18 { 19 arr.Add(strSpan.Slice(0, n).ToString()); 20 strSpan = strSpan.Slice(n + splitSapn.Length); 21 } 22 else 23 { 24 break; 25 } 26 } 27 string.Join(replaceStr, arr); 28 } 29 }); 30 31 Console.WriteLine($"Span.Replace {count} 次用时:{t2} 毫秒 ");
怎么样,经过上面的测试,是不是觉的Span
一、String.Contains 测试
1 var t1 = StopWacherHelper.DoTimer(() => 2 { 3 for (int i = 0; i < count; i++) 4 { 5 str.Contains(subStr); 6 } 7 }); 8 9 Console.WriteLine($"string.Contains {count} 次用时:{t1} 毫秒");
1 var t2 = StopWacherHelper.DoTimer(() => 2 { 3 var strSpan = str.AsSpan(); 4 5 var subStrSpan = subStr.AsSpan(); 6 7 for (int i = 0; i < count; i++) 8 { 9 strSpan.Contains(subStrSpan, StringComparison.CurrentCulture); 10 } 11 }); 12 13 Console.WriteLine($"Span.Contains {count} 次用时:{t2} 毫秒 ");
仔细一看,不对啊,Span
二、String.Compare 测试
1 var t1 = StopWacherHelper.DoTimer(() => 2 { 3 for (int i = 0; i < count; i++) 4 { 5 string.Compare(str, subStr, true); 6 } 7 }); 8 9 Console.WriteLine($"string.Compare {count} 次用时:{t1} 毫秒");
1 var t2 = StopWacherHelper.DoTimer(() => 2 { 3 var strSpan = str.AsSpan(); 4 5 var subStrSpan = subStr.AsSpan(); 6 7 for (int i = 0; i < count; i++) 8 { 9 strSpan.Contains(subStrSpan, StringComparison.CurrentCultureIgnoreCase); 10 } 11 }); 12 13 Console.WriteLine($"Span.Contains {count} 次用时:{t2} 毫秒 ");
还是被反杀了,这说明Span
如果对上述感兴趣,可以点击这里下载测试源码,更多请点击左下角的推荐,谢谢~