Skip to content

Commit

Permalink
Longer strings for string.Replace(char, char) benchmarks (#2329)
Browse files Browse the repository at this point in the history
* Longer strings for string.Replace(char, char) benchmarks

* Update src/benchmarks/micro/libraries/System.Runtime/Perf.String.cs

Co-authored-by: Dan Moseley <[email protected]>

* Use a random string

Co-authored-by: Dan Moseley <[email protected]>
  • Loading branch information
gfoidl and danmoseley authored Mar 24, 2022
1 parent 7d5a03b commit 38fcdf0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/benchmarks/micro/libraries/CommonUtils/PerfUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Linq;

namespace System
{
Expand Down Expand Up @@ -31,5 +32,15 @@ public static string CreateString(int length)

return new string(str);
}

/// <summary>
/// Helper method to create a string containing random alphanumeric
/// characters equal to the specified length
/// </summary>
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());
}
}
}
15 changes: 11 additions & 4 deletions src/benchmarks/micro/libraries/System.Runtime/Perf.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object[]> 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
Expand Down

0 comments on commit 38fcdf0

Please sign in to comment.