From 38fcdf02f3fa35bfbe02cff9aa711b459ca326b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Fri, 25 Mar 2022 00:14:25 +0100 Subject: [PATCH] Longer strings for string.Replace(char, char) benchmarks (#2329) * Longer strings for string.Replace(char, char) benchmarks * Update src/benchmarks/micro/libraries/System.Runtime/Perf.String.cs Co-authored-by: Dan Moseley * Use a random string Co-authored-by: Dan Moseley --- .../micro/libraries/CommonUtils/PerfUtils.cs | 11 +++++++++++ .../micro/libraries/System.Runtime/Perf.String.cs | 15 +++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/benchmarks/micro/libraries/CommonUtils/PerfUtils.cs b/src/benchmarks/micro/libraries/CommonUtils/PerfUtils.cs index 1464e7370f2..630ecd967c7 100644 --- a/src/benchmarks/micro/libraries/CommonUtils/PerfUtils.cs +++ b/src/benchmarks/micro/libraries/CommonUtils/PerfUtils.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.IO; +using System.Linq; namespace System { @@ -31,5 +32,15 @@ public static string CreateString(int length) return new string(str); } + + /// + /// Helper method to create a string containing random alphanumeric + /// characters equal to the specified length + /// + public static string CreateRandomString(int length, string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789", int seed = 42) + { + Random random = new(seed); // use the given seed, to make the benchmarks repeatable + return new string(Enumerable.Repeat(alphabet, length).Select(s => s[random.Next(s.Length)]).ToArray()); + } } } diff --git a/src/benchmarks/micro/libraries/System.Runtime/Perf.String.cs b/src/benchmarks/micro/libraries/System.Runtime/Perf.String.cs index 28e8bd0b037..4a4646e7c61 100644 --- a/src/benchmarks/micro/libraries/System.Runtime/Perf.String.cs +++ b/src/benchmarks/micro/libraries/System.Runtime/Perf.String.cs @@ -154,13 +154,20 @@ public string TrimEnd_CharArr(string s, char[] c) => s.TrimEnd(c); [Benchmark] - [Arguments("Hello", 'l', '!')] // Contains two 'l' - [Arguments("Hello", 'a', 'b')] // Contains one 'a' - [Arguments("This is a very nice sentence", 'z', 'y')] // 'z' does not exist in the string - [Arguments("This is a very nice sentence", 'i', 'I')] // 'i' occuress 3 times in the string + [ArgumentsSource(nameof(ReplaceArguments))] public string Replace_Char(string text, char oldChar, char newChar) => text.Replace(oldChar, newChar); + public static IEnumerable ReplaceArguments() + { + yield return new object[] { "Hello", 'l', '!' }; // Contains two 'l' + yield return new object[] { "Hello", 'a', 'b' }; // Contains one 'a' + yield return new object[] { "This is a very nice sentence", 'z', 'y' }; // 'z' does not exist in the string + yield return new object[] { "This is a very nice sentence", 'i', 'I' }; // 'i' occurs 3 times in the string + yield return new object[] { PerfUtils.CreateRandomString(100, seed: 42), 'b', '+' }; // b occurs 8 times in the string + yield return new object[] { PerfUtils.CreateRandomString(1000, seed: 42), 'b', '+' }; // b occurs 42 times in the string + } + [Benchmark] [Arguments("This is a very nice sentence", "bad", "nice")] // there are no "bad" words in the string [Arguments("This is a very nice sentence", "nice", "bad")] // there are is one "nice" word in the string