public void TestTwoSum() { var numList = new List <int>(); for (int i = 0; i < 55; i++) { for (int j = 0; j < 10; j++) { numList.Add(i); numList.Add(-i); } } var nums = numList.ToArray(); var times = 10000; var s2 = TwoSum(nums, target, 0); var se = TwoSumEnumerable(nums, target, 0); var hashS = TwoSumWithHashSet(nums, target, 0); Assert.AreEqual(s2.Count, se.Count()); Assert.AreEqual(hashS.Count, s2.Count); var tsTwoSum = StopwatchHelper.Calculate(times, () => { var resNums = TwoSum(nums, target, 0); var sum = 0; for (int i = 0; i < resNums.Count; i++) { for (int j = 0; j < resNums[i].Count; j++) { sum += resNums[i][j]; } } }); var tsTwoSumE = StopwatchHelper.Calculate(times, () => { var resNums = TwoSumEnumerable(nums, target, 0); var sum = 0; sum += resNums.SelectMany(p => p).Sum(); }); var tsTwoSumHash = StopwatchHelper.Calculate(times, () => { var resNums = TwoSumWithHashSet(nums, target, 0); var sum = 0; for (int i = 0; i < resNums.Count; i++) { for (int j = 0; j < resNums[i].Count; j++) { sum += resNums[i][j]; } } }); Trace.WriteLine($"TwoSum : {tsTwoSum.TotalMilliseconds}"); Trace.WriteLine($"TwoSumE : {tsTwoSumE.TotalMilliseconds}"); Trace.WriteLine($"TwoSumHash : {tsTwoSumHash.TotalMilliseconds}"); }
public void TestGenerateParenthesis() { var n = 8; var times = 1000; var ts0 = StopwatchHelper.Calculate(times, () => { var s = GenerateParenthesis(n); }); Trace.WriteLine($"Old:{ts0.TotalMilliseconds}"); var ts2 = StopwatchHelper.Calculate(times, () => { var s = GenerateParenthesis2(n); }); Trace.WriteLine($"New:{ts2.TotalMilliseconds}"); }
public void TestLetterCombinations() { var str = "2344488"; var times = 10000; var ts0 = StopwatchHelper.Calculate(times, () => { var s = LetterCombinations(str); }); Trace.WriteLine($"Old : {ts0.TotalMilliseconds}"); var ts1 = StopwatchHelper.Calculate(times, () => { var s = LetterCombinations2(str); }); Trace.WriteLine($"New : {ts1.TotalMilliseconds}"); }
public void TestMedian() { var nums1 = new int[] { 1, 2, 3 }; var nums2 = new int[] { 1, 2, 3, 4, 5, 6 }; var times = 1000000; var ts0 = StopwatchHelper.Calculate(times, () => FindMedianSortedArrays(nums1, nums2)); Trace.WriteLine(ts0.TotalMilliseconds); var ts1 = StopwatchHelper.Calculate(times, () => FindMedianSortedArrays2(nums1, nums2)); Trace.WriteLine(ts1.TotalMilliseconds); var ts2 = StopwatchHelper.Calculate(times, () => FindMedianSortedArrays3(nums1, nums2)); Trace.WriteLine(ts2.TotalMilliseconds); }
public void TestThreeSum() { var nums = new int[] { -1, 0, 1, 2, -1, -2, 2, -4, -4, 4, -2 }; var times = 1000000; //var ts = StopwatchHelper.Calculate(times, () => ThreeSum2(nums)); //Trace.WriteLine(ts.TotalMilliseconds); //var ts2 = StopwatchHelper.Calculate(times, () => ThreeSum(nums)); //Trace.WriteLine(ts2.TotalMilliseconds); var ss = ThreeSum3(nums); Assert.AreEqual(ThreeSum3(nums).Count, 6); var ts3 = StopwatchHelper.Calculate(times, () => ThreeSum3(nums)); Trace.WriteLine(ts3.TotalMilliseconds); }
public void TestLongestPalindrome() { var times = 1000; var ts1 = StopwatchHelper.Calculate(times, () => { Assert.AreEqual(LongestPalindrome2("babad"), "bab"); Assert.AreEqual(LongestPalindrome2("cbbd"), "bb"); }); Trace.WriteLine($"New:{ts1.TotalMilliseconds}"); var ts2 = StopwatchHelper.Calculate(times, () => { Assert.AreEqual(LongestPalindrome("babad"), "bab"); Assert.AreEqual(LongestPalindrome("cbbd"), "bb"); }); Trace.WriteLine($"Origin:{ts2.TotalMilliseconds}"); }
public void PerformanceReportWithNoArguments() { int count = 1000000; Trace.WriteLine($"#{count} 次调用:"); double time = StopwatchHelper.Calculate(count, () => { ClassB b = new ClassB(); }).TotalMilliseconds; Trace.WriteLine($"‘New’耗时 {time} milliseconds"); double time2 = StopwatchHelper.Calculate(count, () => { ClassB b = CreateObjectFactory.CreateInstance2 <ClassB>(); }).TotalMilliseconds; Trace.WriteLine($"‘Emit 工厂’耗时 {time2} milliseconds"); double time3 = StopwatchHelper.Calculate(count, () => { ClassB b = ExpressionCreateObject.CreateInstance <ClassB>(); }).TotalMilliseconds; Trace.WriteLine($"‘Expression’耗时 {time3} milliseconds"); double time4 = StopwatchHelper.Calculate(count, () => { ClassB b = ExpressionCreateObjectFactory.CreateInstance <ClassB>(); }).TotalMilliseconds; Trace.WriteLine($"‘Expression 工厂’耗时 {time4} milliseconds"); double time5 = StopwatchHelper.Calculate(count, () => { ClassB b = Activator.CreateInstance <ClassB>(); //ClassB b = Activator.CreateInstance(typeof(ClassB)) as ClassB; }).TotalMilliseconds; Trace.WriteLine($"‘Activator.CreateInstance’耗时 {time5} milliseconds"); /** * #1000000 次调用: * * ‘New’耗时 21.7474 milliseconds * * ‘Emit 工厂’耗时 174.088 milliseconds * * ‘Expression’耗时 42.9405 milliseconds * * ‘Expression 工厂’耗时 162.548 milliseconds * * ‘Activator.CreateInstance’耗时 67.3712 milliseconds * * */ }
public void PerformanceReportWithArguments() { int count = 1000000; Trace.WriteLine($"#{count} 次调用:"); double time = StopwatchHelper.Calculate(count, () => { ClassA a = new ClassA(new ClassB()); }).TotalMilliseconds; Trace.WriteLine($"‘New’耗时 {time} milliseconds"); double time2 = StopwatchHelper.Calculate(count, () => { ClassA a = CreateObjectFactory.CreateInstance2 <ClassA, ClassB>(new ClassB()); }).TotalMilliseconds; Trace.WriteLine($"‘Emit 工厂’耗时 {time2} milliseconds"); double time4 = StopwatchHelper.Calculate(count, () => { ClassA a = ExpressionCreateObjectFactory.CreateInstance <ClassA>(new ClassB()); }).TotalMilliseconds; Trace.WriteLine($"‘Expression 工厂’耗时 {time4} milliseconds"); double time5 = StopwatchHelper.Calculate(count, () => { ClassA a = Activator.CreateInstance(typeof(ClassA), new ClassB()) as ClassA; }).TotalMilliseconds; Trace.WriteLine($"‘Activator.CreateInstance’耗时 {time5} milliseconds"); double time6 = StopwatchHelper.Calculate(count, () => { var a = EmitStateCell <ClassB, ClassA> .CreateObjectHandler.Invoke(new ClassB()); }).TotalMilliseconds; Trace.WriteLine($"Emit Object工厂耗时:{time6}"); // /** // #1000000 次调用: // ‘New’耗时 29.3612 milliseconds // ‘Emit 工厂’耗时 634.2714 milliseconds // ‘Expression 工厂’耗时 620.2489 milliseconds // ‘Activator.CreateInstance’耗时 588.0409 milliseconds // * */ //} }