DotNETWeekly-io / DotNetWeekly

DotNet weekly newsletter
MIT License
198 stars 3 forks source link

【文章推荐】Hanselman & Toub 现场代码秀 #659

Closed gaufung closed 1 month ago

gaufung commented 1 month ago

https://www.youtube.com/watch?v=TRFfTdzpk-M&ab_channel=MicrosoftDeveloper

gaufung commented 1 month ago

今年 Build 大会,Scott HanselmanStephen Toub 这对老搭档又来整活,他们通过对 Humanizer 这个库的优化,来提升 C# 代码性能。

- value.Substring(0, length - truncationString.Length) + truncationString
+ String.Concat(value.AspSpan(0,  length - truncationString.Length), truncationString.AsSpan())
- public static string Transform(this string input, params IStringTransformer[] transformers)
+ public static string Transform(this string input params ReadonlySpan<IStringTransformer> transformers)
- private static readonly Regex ValidRomanNumeral =  new Regex("^(?i:(?=[MDCLXVI])((M{0,3})((C[DM])|(D?C{0,3}))?((X[LC])|(L?XX{0,2})|L)?((I[VX])|(V?(II{0,2}))|V)?))$", RegexOptionsUtil.Compiled);
+ [GeneratedRegex("^(?i:(?=[MDCLXVI])((M{0,3})((C[DM])|(D?C{0,3}))?((X[LC])|(L?XX{0,2})|L)?((I[VX])|(V?(II{0,2}))|V)?))$", RegexOptions.IgnroeCase)]
+ private static partial Regex ValidRomanNumeral();
- private static readonly IDictionary<string, int> RomanNumerals =
-    new Dictionary<string, int>(NumberOfRomanNumeralMaps){
-        {"M", 1000},  {"D", 500},  {"C", 100},  {"L", 50},  {"X", 10}, , {"V", 5},   {"I", 1}};
+ private static int GetValue(char c) =>(c & 0x20) switch {
+    'M' => 1000, 'D' => 500, 'C' => 100, 'L' => 50,
+    'X' => 10,   'V' => 5,   'I' => 1 };