首先,非常感谢赵老大的CodeTimer,它让我们更好的了解到代码执行的性能,从而可以让我们从性能的角度来考虑问题,有些东西可能我们认为是这样的,但经理测试并非如何,这正应了我之前的那名话:“机器最能证明一切”!

费话就不说了,看代码吧:

  1     /// <summary>
2 /// 执行代码规范
3 /// </summary>
4 public interface IAction
5 {
6 void Action();
7 }
8
9 /// <summary>
10 /// 老赵的性能测试工具
11 /// </summary>
12 public static class CodeTimer
13 {
14 [DllImport("kernel32.dll", SetLastError = true)]
15 static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime, out long lpExitTime, out long lpKernelTime, out long lpUserTime);
16
17 [DllImport("kernel32.dll")]
18 static extern IntPtr GetCurrentThread();
19 public delegate void ActionDelegate();
20 private static long GetCurrentThreadTimes()
21 {
22 long l;
23 long kernelTime, userTimer;
24 GetThreadTimes(GetCurrentThread(), out l, out l, out kernelTime, out userTimer);
25 return kernelTime + userTimer;
26 }
27 static CodeTimer()
28 {
29 Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
30 Thread.CurrentThread.Priority = ThreadPriority.Highest;
31 }
32 public static void Time(string name, int iteration, ActionDelegate action)
33 {
34 if (String.IsNullOrEmpty(name))
35 {
36 return;
37 }
38 if (action == null)
39 {
40 return;
41 }
42
43 //1. Print name
44 ConsoleColor currentForeColor = Console.ForegroundColor;
45 Console.ForegroundColor = ConsoleColor.Yellow;
46 Console.WriteLine(name);
47
48 // 2. Record the latest GC counts
49 //GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
50 GC.Collect(GC.MaxGeneration);
51 int[] gcCounts = new int[GC.MaxGeneration + 1];
52 for (int i = 0; i <= GC.MaxGeneration; i++)
53 {
54 gcCounts[i] = GC.CollectionCount(i);
55 }
56
57 // 3. Run action
58 Stopwatch watch = new Stopwatch();
59 watch.Start();
60 long ticksFst = GetCurrentThreadTimes(); //100 nanosecond one tick
61 for (int i = 0; i < iteration; i++) action();
62 long ticks = GetCurrentThreadTimes() - ticksFst;
63 watch.Stop();
64
65 // 4. Print CPU
66 Console.ForegroundColor = currentForeColor;
67 Console.WriteLine("\tTime Elapsed:\t\t" +
68 watch.ElapsedMilliseconds.ToString("N0") + "ms");
69 Console.WriteLine("\tTime Elapsed (one time):" +
70 (watch.ElapsedMilliseconds / iteration).ToString("N0") + "ms");
71 Console.WriteLine("\tCPU time:\t\t" + (ticks * 100).ToString("N0")
72 + "ns");
73 Console.WriteLine("\tCPU time (one time):\t" + (ticks * 100 /
74 iteration).ToString("N0") + "ns");
75
76 // 5. Print GC
77 for (int i = 0; i <= GC.MaxGeneration; i++)
78 {
79 int count = GC.CollectionCount(i) - gcCounts[i];
80 Console.WriteLine("\tGen " + i + ": \t\t\t" + count);
81 }
82 Console.WriteLine();
83 }
84
85
86
87 public static void Time(string name, int iteration, IAction action)
88 {
89 if (String.IsNullOrEmpty(name))
90 {
91 return;
92 }
93
94 if (action == null)
95 {
96 return;
97 }
98
99 //1. Print name
100 ConsoleColor currentForeColor = Console.ForegroundColor;
101 Console.ForegroundColor = ConsoleColor.Yellow;
102 Console.WriteLine(name);
103
104 // 2. Record the latest GC counts
105 //GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
106 GC.Collect(GC.MaxGeneration);
107 int[] gcCounts = new int[GC.MaxGeneration + 1];
108 for (int i = 0; i <= GC.MaxGeneration; i++)
109 {
110 gcCounts[i] = GC.CollectionCount(i);
111 }
112
113 // 3. Run action
114 Stopwatch watch = new Stopwatch();
115 watch.Start();
116 long ticksFst = GetCurrentThreadTimes(); //100 nanosecond one tick
117 for (int i = 0; i < iteration; i++) action.Action();
118 long ticks = GetCurrentThreadTimes() - ticksFst;
119 watch.Stop();
120
121 // 4. Print CPU
122 Console.ForegroundColor = currentForeColor;
123 Console.WriteLine("\tTime Elapsed:\t\t" +
124 watch.ElapsedMilliseconds.ToString("N0") + "ms");
125 Console.WriteLine("\tTime Elapsed (one time):" +
126 (watch.ElapsedMilliseconds / iteration).ToString("N0") + "ms");
127 Console.WriteLine("\tCPU time:\t\t" + (ticks * 100).ToString("N0")
128 + "ns");
129 Console.WriteLine("\tCPU time (one time):\t" + (ticks * 100 /
130 iteration).ToString("N0") + "ns");
131
132 // 5. Print GC
133 for (int i = 0; i <= GC.MaxGeneration; i++)
134 {
135 int count = GC.CollectionCount(i) - gcCounts[i];
136 Console.WriteLine("\tGen " + i + ": \t\t\t" + count);
137 }
138 Console.WriteLine();
139 }
140 }

有了上面的codeTimer我们就来测试一个吧,如字条串和并的问题,用+=还是用StringBuilder呢,有点经验的程序员肯定说是StringBuilder,是的,确实是后者,那我们就来看看这

两种方法测试的结果吧

 1      CodeTimer.Time("String  Concat", 100000,
2 () =>
3 {
4 var s = "1";
5 for (int i = 1; i < 10; i++)
6 s = s + "1";
7 });
8
9 CodeTimer.Time("StringBuilder Concat", 100000,
10 () =>
11 {
12 var s = new StringBuilder();
13 for (int i = 1; i < 10; i++)
14 s.Append("1");
15 });

测试的结果如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaUAAAFFCAIAAADD9GV7AAAd+UlEQVR4nO2c27cdRZ2A+2HWmiWBGQFRB50RyAVCgIRLAhFy4R4kQMJVEuAkEIGEJOfkYgwISbhKuCThqoI6KgqKg6hBcMSRDehaPvrgv+Fa8zhr5mHPQ+3TVLqqfl193d1V37fqoXZ17fpVdVV/p3rv3if521//8re//uWoWZtKpxmzN82YfZ9KR8+57+g5m4+es/noUzcfc+rmY07dcsypW445bcs/qTR36z+n6fTJT6s0b/LT8yaPnTd17LypY8+YOvaMqePO2HbcGduOO3PbcWduO/7M7ceftf34s7Z/RqX5Oz4zf8cJ83ecsGDHCQt2nrBg52cX7Pzs2Sp9/XPnjNLnz9n1+XNH6V/O+0aaTlz4jRMX7j5x4e4TF+3+wqLdX1h0/xcW3f/F81V64IvnP/CvF0ynxd/8tzR9+cEvqXThgydd+OBJFz500oUPnXTRQydf9NDJF+05ecmek5fsOWXJnlOW7D1l6d5Tlu6duXTvzGV7Zy7bN3PZvlnL9s1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,无论是执行时间,还是对CPU的消耗及GC回收都远低于String的拼结,所以,才有以下结论:

在字符串拼结时,请使用StringBuilder吧!

将不确定变为确定~老赵写的CodeTimer是代码性能测试的利器的更多相关文章

  1. 根据老赵轻量级Actor进行修改的Actor模型

    学习了老赵轻量级Actor模型,并在实际中使用,效果不错. 老赵轻量级Actor模型: ActorLite:一个轻量级Actor模型实现(上) ActorLite:一个轻量级Actor模型实现(中) ...

  2. 老赵点滴地址:http://blog.zhaojie.me/2009/05/a-simple-actor-model-implementation.html

    老赵点滴地址:http://blog.zhaojie.me/2009/05/a-simple-actor-model-implementation.html

  3. python实现统计你一共写了多少行代码

    程序员要保证一定的代码量就必须勤奋的敲代码,但怎么知道自己一共写了多少代码呢,笔者用python写了个简单的脚本,遍历所有的.java,.cpp,.c文件的行数,但是正如大家所知,java生成了许多代 ...

  4. 如何写出无法维护的代码(JAVA版)

    程序命名(针对那些不能混淆的代码) 容易输入的名字.比如:Fred,asdf 单字母的变量名.比如:a,b,c, x,y,z,或者干脆上中文比如(阿隆索肯德基) 有创意地拼写错误.比如:SetPint ...

  5. 写出优质Java代码的4个技巧

    我们平时的编程任务不外乎就是将相同的技术套件应用到不同的项目中去,对于大多数情况来说,这些技术都是可以满足目标的.然而,有的项目可能需要用到一些特别的技术,因此工程师们得深入研究,去寻找那些最简单但最 ...

  6. 写出优质Java代码的4个技巧(转)

    http://geek.csdn.net/news/detail/238243 原文:4 More Techniques for Writing Better Java 作者:Justin Alban ...

  7. 用bochs调试自己写的系统引导代码

    1 安装和配置bochs 首先从bochs.sourceforge.net里面把BOCHS给download下来,鉴于Windows的普及,仅仅谈BOCHS在win下的使用方法,其实在其它的OS中方法 ...

  8. 你写的什么垃圾代码让Vsync命令不能及时处理呢?(1)

    想想自己写的什么垃圾代码导致Vsync不能及时处理#(不高兴) 想不开? 实际开发中性能问题不好复现?这你就可能需要一些工具来帮你检测这种情况. 首先是Android系统自带的工具(4.1之后的版本) ...

  9. Linux Kernel文件系统写I/O流程代码分析(二)bdi_writeback

    Linux Kernel文件系统写I/O流程代码分析(二)bdi_writeback 上一篇# Linux Kernel文件系统写I/O流程代码分析(一),我们看到Buffered IO,写操作写入到 ...

随机推荐

  1. urlrewrite的rule怎么写

    一.正则表达式教程 常用正则如下: .              换行符以外的所有字符 \w          匹配字母或数字或下划线或汉字 \s           匹配任意的空白符 \d     ...

  2. (PMP)第7章-----项目成本管理

    7.1 规划成本管理 输入 工具与技术 输出 1.项目章程 2.项目管理计划 (进度管理计划, 风险管理计划) 3.事业环境因素 4.组织过程资产 1.专家判断 2.数据分析 3.会议 1.成本管理计 ...

  3. android-基础编程-ExpandableListview

    ExpandableListView继承ListView,具有LIstVIew的基本功能.此外具有group/child,由组与子元素组成. 1.布局主要有是三个. a.主布局: <Expand ...

  4. 深度搜索优先(全排列)//本内容来自《啊哈!算法》或者英文名《Aha!Algorithms》)

      package Mypackage; import java.util.Scanner; public class 全排列{ static int a[]=new int[10]; static ...

  5. 程序的流程控制-分支结构 if

    1.分支结构:if语句 第一种格式: /* if(条件表达式){ 语句体; } 其它语句 */ public class IfDemo1{ public static void main(String ...

  6. Integer Array Ladder questions

    1.这个题不难,关键在于把题目意思理解好了.这个题问的不清楚.要求return new length,很容易晕掉.其实就是return 有多少个单独的数. import java.util.Array ...

  7. 利用Go2Shell 实现 Mac Finder 直接shell端打开当前文件夹

    Finder 窗口 ,点击下图所示的按钮(红色框内),即可打开Shell Terminal. 打开后,如图 用法 安装go2shell后,打开finder的application文件夹,找到go2sh ...

  8. 【算法】二叉查找树(BST)实现字典API

    参考资料 <算法(java)>                           — — Robert Sedgewick, Kevin Wayne <数据结构>       ...

  9. SqlSessionFactoryBean的构建流程

    目的 此文的主旨在于梳理SqlSessionFactoryBean的初始流程,不拘泥于实现细节. 使用 SqlSessionFactoryBean的主要作用便是用来创建SqlSessionFactor ...

  10. Swift5 语言指南(七) 集合类型

    Swift提供三种主要的集合类型,称为数组,集合和字典,用于存储值集合.数组是有序的值集合.集是唯一值的无序集合.字典是键值关联的无序集合. Swift中的数组,集合和字典总是清楚它们可以存储的值和键 ...