方法性能比较的好帮手:CodeTimer的扩展应用
2010-01-13 12:03
429 查看
在09年的.NET技术大会上Jeffery Richard展示的性能测试代码中,功能强大的CodeTimer引起了大家的注意,由于Jeffery Richard还没有公布源码,于是园子里的老赵就自己写了一个,适用于在Console应用程序中输出性能测试的各个指标。
最近参与的项目中有很多想要进行方法性能比较的地方,于是就想起了这个CodeTimer,便狗尾续貂在老赵原有的代码上加上了一个Time方法的重载,以适用于非Console应用程序中快速的性能测试,比如Winform、Web、UnitTest、WindowsService等等。
修改后的代码如下:
这样一来,就能够在Winform、Web、UnitTest、Service等各种应用程序中方便快捷的使用CodeTimer了,比如在Winform中的一个应用:
最近参与的项目中有很多想要进行方法性能比较的地方,于是就想起了这个CodeTimer,便狗尾续貂在老赵原有的代码上加上了一个Time方法的重载,以适用于非Console应用程序中快速的性能测试,比如Winform、Web、UnitTest、WindowsService等等。
修改后的代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Diagnostics; using System.Threading; namespace PerformanceTest { public static class CodeTimer { public static void Initialize() { Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; Thread.CurrentThread.Priority = ThreadPriority.Highest; Time("", 1, () => { }); } public static void Time(string name, int iteration, Action action) { Time(name, iteration, action, Console.WriteLine); } public static void Time(string name, int iteration, Action action, Action<string> output) { if (String.IsNullOrEmpty(name)) return; // 1. ConsoleColor currentForeColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; output(name); // 2. GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); int[] gcCounts = new int[GC.MaxGeneration + 1]; for (int i = 0; i <= GC.MaxGeneration; i++) { gcCounts[i] = GC.CollectionCount(i); } // 3. Stopwatch watch = new Stopwatch(); watch.Start(); ulong cycleCount = GetCycleCount(); for (int i = 0; i < iteration; i++) action(); ulong cpuCycles = GetCycleCount() - cycleCount; watch.Stop(); // 4. Console.ForegroundColor = currentForeColor; output("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms"); output("\tCPU Cycles:\t" + cpuCycles.ToString("N0")); // 5. for (int i = 0; i <= GC.MaxGeneration; i++) { int count = GC.CollectionCount(i) - gcCounts[i]; output("\tGen " + i + ": \t\t" + count); } output(string.Empty); } private static ulong GetCycleCount() { ulong cycleCount = 0; QueryThreadCycleTime(GetCurrentThread(), ref cycleCount); return cycleCount; } [DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime); [DllImport("kernel32.dll")] static extern IntPtr GetCurrentThread(); } }
这样一来,就能够在Winform、Web、UnitTest、Service等各种应用程序中方便快捷的使用CodeTimer了,比如在Winform中的一个应用:
StringBuilder result; private void button1_Click(object sender, EventArgs e) { string s = string.Empty; PerformanceTest.CodeTimer.Time("String Concat", 10000, () => { s += "a"; }, Print); StringBuilder sb = new StringBuilder(); PerformanceTest.CodeTimer.Time("StringBuilder", 10000, () => { sb.Append("a"); }, Print); this.textBox1.Text = result.ToString(); } void Print(string msg) { if (result == null) result = new StringBuilder(); result.AppendLine(msg); Console.WriteLine(msg); }
相关文章推荐
- 谈谈CListCtrl 扩展风格设置方法-SetExtendedStyle和ModifyStyleEx 比较
- 获取手机内部所有应用的方法比较 PackageInfo、ResolveInfo
- 各种同步方法性能比较(synchronized,ReentrantLock,Atomic)
- c#扩展方法奇思妙用高级篇三:Enumerable.Cast<T>应用
- 遍历Map和List的几种方法和性能比较
- 一之续、A*,Dijkstra,BFS算法性能比较及A*算法的应用
- java java中subString、split、stringTokenizer三种截取字符串方法的性能比较
- 计算fibnacci数列第n个数的几种方法以及性能比较
- 分页实现方法的性能比较
- Spring/Hibernate 应用性能优化的7种方法
- Java中遍历Map的方法及其性能比较
- Spring/Hibernate 应用性能优化的7种方法
- 使用API扩展应用功能必会的方法
- 各种同步方法性能比较(synchronized,ReentrantLock,Atomic)
- <转载>提高 ASP.NET Web 应用性能的 24 种方法和技巧
- [转载]Spring/Hibernate 应用性能优化的7种方法
- Python扩展方法及工具比较
- 一起谈.NET技术,数组排序方法的性能比较(中):Array.Sort<T> 实现分析
- 不同版本的SQL Server之间数据导出导入的方法及性能比较
- 一之续、A*,Dijkstra,BFS算法性能比较及A*算法的应用