Closed udlose closed 2 years ago
Instead you might as well do something like
static bool CheckNumericOnly(string data) {
for (var i = 0; i < data.Length; i++) {
if (!char.IsDigit(data[i])) return false;
}
return data.Length > 0;
}
Instead you might as well do something like
static bool CheckNumericOnly(string data) { for (var i = 0; i < data.Length; i++) { if (!char.IsDigit(data[i])) return false; } return data.Length > 0; }
Might be more clear too.
It is as close to allocation-free as I can think of and slightly faster than using a regex. I considered it a micro-optimization but I'll make the change if you're ok with it.
Just to be clear, all of my perf measurements have been done using .NET 6 as I don't have other versions installed on my dev box.
Fixed in merge of PR https://github.com/barnhill/barcodelib/pull/143
Current usage of
Regex.IsMatch(data, @"^\d+$", RegexOptions.Compiled)
inBarcodeCommon.CheckNumericOnly(string data)
has room for several performance improvements:It uses the
System.Text.RegularExpressions.Regex.IsMatch(string input, string pattern, RegexOptions options)
. Behind the scene, the runtime creates a newRegex
instance (memory allocation) and compiles and caches the Regex. The size of theRegex
cache is only 15 entries per AppDomain. So, depending on what else the app using this nuget package is doing, these newRegex
instances may (or may not) be cached/compiled for every execution ofCheckNumericOnly
.Changing this
Regex
instance to astatic
member ofBarcodeCommon
ensures that only 1 instance ofRegex
is ever created per AppDomain, ensures that regex compilation only occurs once, it is guaranteed to be "cached" since it is a static, and avoids being placed in theSystem.Text.RegularExpressions.Regex
cache allowing the application using this nuget to be unaffected by its own usage of static calls toSystem.Text.RegularExpressions.Regex
.