public void TestReverseOnlyLetters() { var demo = new ReverseOnlyLetters(); var rand = new Random(); for (int i = 0; i < 1000; i++) { var len = rand.Next(10) + 25; char[] arr = new char[len]; for (int j = 0; j < len; j++) { arr[j] = (char)(rand.Next('z') + 65); } var str = new string(arr); //var solution = demo.Solution(str); //var otherSolution = demo.OtherSolution(str); var otherSolution = StopWatchTools.CountTime(() => demo.OtherSolution(str)); var solution = StopWatchTools.CountTime(() => demo.Solution(str)); _output.WriteLine($@" str:{str} solution time:{solution} otherSolution time:{otherSolution} "); // _output.WriteLine($@" //str:{str} //solution:{solution} //otherSolution:{otherSolution} //"); // Assert.Equal(solution, otherSolution); } }
private static void TestNormalVsLinqEach() { var json = @"[]"; var info = JsonConvert.DeserializeObject <List <dynamic> >(json); Action normalAction = () => { foreach (var item in info) { var user = item.user; if (user != null) { user.name = "encode"; } } }; //相差不大且耗时 Action linqAction = () => { info = info.Select(u => { var user = u.user; if (user != null) { user.name = "decode"; } return(u); }).ToList(); }; for (int i = 0; i < 100; i++) { Console.WriteLine($"--------------{i + 1}-----------------"); StopWatchTools.ShowCountTime(normalAction, nameof(normalAction)); StopWatchTools.ShowCountTime(linqAction, nameof(linqAction)); } }