diff --git a/BenchmarkTestBench/BenchmarkTestBench.csproj b/BenchmarkTestBench/BenchmarkTestBench.csproj new file mode 100644 index 0000000..2383364 --- /dev/null +++ b/BenchmarkTestBench/BenchmarkTestBench.csproj @@ -0,0 +1,18 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + + + diff --git a/BenchmarkTestBench/Program.cs b/BenchmarkTestBench/Program.cs new file mode 100644 index 0000000..e383624 --- /dev/null +++ b/BenchmarkTestBench/Program.cs @@ -0,0 +1,44 @@ +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Running; +using CSharpExtender.ExtensionMethods; + +// Select assemblies to benchmark +BenchmarkSwitcher + .FromAssembly(typeof(Program).Assembly) + .Run(args); + +// Create a method in the MyBenchmarks class for each benchmark to perform + +// Uncomment one the following lines to run the benchmarks in faster mode +//[MediumRunJob] +[ShortRunJob] +[MemoryDiagnoser] +public class MyBenchmarks +{ + #region Constants for parameters + + private const string LONG_NUMERIC_STRING = + @" +1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 +1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 +1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 +1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 +1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"; + + #endregion + + [Params("1234567890", "1234567890a", "1234567890!@#$%^&*()_+", LONG_NUMERIC_STRING)] + public string _testString = ""; + + [GlobalSetup] + public void Setup() + { + // Setup code here + } + + //[Benchmark] + public void Benchmark_IsDigitOnly() + { + var test = _testString.IsDigitsOnly(); + } +} diff --git a/CSharpExtender.sln b/CSharpExtender.sln index 6eb83ce..6aa96fb 100644 --- a/CSharpExtender.sln +++ b/CSharpExtender.sln @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpExtender", "CSharpExt EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.CSharpExtender", "Test.CSharpExtender\Test.CSharpExtender.csproj", "{E58959C9-A22B-4B5D-9507-B6A2CD8B2607}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkTestBench", "BenchmarkTestBench\BenchmarkTestBench.csproj", "{1CF1DAEF-0755-4F06-89CD-6ABCF50A838C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {E58959C9-A22B-4B5D-9507-B6A2CD8B2607}.Debug|Any CPU.Build.0 = Debug|Any CPU {E58959C9-A22B-4B5D-9507-B6A2CD8B2607}.Release|Any CPU.ActiveCfg = Release|Any CPU {E58959C9-A22B-4B5D-9507-B6A2CD8B2607}.Release|Any CPU.Build.0 = Release|Any CPU + {1CF1DAEF-0755-4F06-89CD-6ABCF50A838C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1CF1DAEF-0755-4F06-89CD-6ABCF50A838C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1CF1DAEF-0755-4F06-89CD-6ABCF50A838C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1CF1DAEF-0755-4F06-89CD-6ABCF50A838C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CSharpExtender/ExtensionMethods/StringExtensionMethods.cs b/CSharpExtender/ExtensionMethods/StringExtensionMethods.cs index f8f3a32..8e0efcb 100644 --- a/CSharpExtender/ExtensionMethods/StringExtensionMethods.cs +++ b/CSharpExtender/ExtensionMethods/StringExtensionMethods.cs @@ -72,8 +72,20 @@ public static string ToDigitsOnly(this string value) => /// /// /// - public static bool IsDigitsOnly(this string s) => - !s.Any(c => !char.IsDigit(c)); + public static bool IsDigitsOnly(this string s) + { + ReadOnlySpan span = s.AsSpan(); + + for (int i = 0; i < span.Length; i++) + { + if (!char.IsDigit(span[i])) + { + return false; + } + } + + return true; + } /// /// Converts an IEnumerable of strings to a single string with line feeds between each string