CodeTimer
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading; namespace IOCPerformanceTest.Core
{
public sealed class CodeTimer
{
public static void Initialize()
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Time("", 1, () => { });
} public static void Time(string name, Action action)
{
Time(name, 1, action);
} public static void Time(string name, int iteration, Action action)
{
if (String.IsNullOrEmpty(name)) return; // 1.
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(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();
long cycleCount = GetCycleCount();
for (int i = 0; i < iteration; i++) action();
long cpuCycles = GetCycleCount() - cycleCount;
watch.Stop(); // 4.
Console.ForegroundColor = currentForeColor;
Console.WriteLine("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
Console.WriteLine("\tCPU Cycles:\t" + cpuCycles.ToString("N0")); // 5.
for (int i = 0; i <= GC.MaxGeneration; i++)
{
int count = GC.CollectionCount(i) - gcCounts[i];
Console.WriteLine("\tGen " + i + ": \t\t" + count);
} Console.WriteLine(); } private static long GetCycleCount()
{
//ulong cycleCount = 0;
//QueryThreadCycleTime(GetCurrentThread(), ref cycleCount);
//return cycleCount;
return GetCurrentThreadTimes();
} [DllImport("kernel32.dll", SetLastError = true)]
static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime,
out long lpExitTime, out long lpKernelTime, out long lpUserTime); private static long GetCurrentThreadTimes()
{
long l;
long kernelTime, userTimer;
GetThreadTimes(GetCurrentThread(), out l, out l, out kernelTime,
out userTimer);
return kernelTime + userTimer;
} //[DllImport("kernel32.dll")]
//[return: MarshalAs(UnmanagedType.Bool)]
//static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime); [DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread(); }
}
CodeTimer的更多相关文章
- 将不确定变为确定~老赵写的CodeTimer是代码性能测试的利器
首先,非常感谢赵老大的CodeTimer,它让我们更好的了解到代码执行的性能,从而可以让我们从性能的角度来考虑问题,有些东西可能我们认为是这样的,但经理测试并非如何,这正应了我之前的那名话:“机器最能 ...
- CodeTimer 代码性能计数器
收集整理老赵 的”CodeTimer“. 用于测试代码性能.详见可参考 老赵原文 代码如下: using System; using System.Diagnostics; using System. ...
- 一个简单的性能计数器:CodeTimer
public static class CodeTimer { public static void Initialize() { Process.GetCurrentProcess().Priori ...
- .NET基础拾遗(3)字符串、集合和流
Index: (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基础 ...
- Attribute操作的性能优化方式
Attribute是.NET平台上提供的一种元编程能力,可以通过标记的方式来修饰各种成员.无论是组件设计,语言之间互通,还是最普通的框架使 用,现在已经都离不开Attribute了.迫于Attribu ...
- MongoDB 分页查询的方法及性能
最近有点忙,本来有好多东西可以总结,Redis系列其实还应该有四.五.六...不过<Redis in Action>还没读完,等读完再来总结,不然太水,对不起读者. 自从上次Redis之后 ...
- 剑指Offer面试题:29.丑数
一.题目:丑数 题目:我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因为它包含因子7.习惯上我们把1当做第一个 ...
- Loogn.OrmLite映射优化记录
大家对ORM效率的争议多半在映射性能方面.自己的ORMLite也是如此,经过前段时间的折腾,已经找不出一个简单的方法再提升一下这部分的方法了.在此把优化涉及的几点记录一下. 注:用于性能测试的Code ...
- 各大主流.Net的IOC框架性能测试比较
Autofac下载地址:http://code.google.com/p/autofac/ Castle Windsor下载地址:http://sourceforge.net/projects/cas ...
随机推荐
- 转:ie6与firefox操作iframe中DOM节点的一点不同
依次在两个浏览器中运行以下代码 <html> <body> <iframe id="myiframe"></iframe> < ...
- eWebeditor编辑器上传图片的设置程序
protected void Button1_Click(object sender, EventArgs e) { if (this.uploadfile.PostedFile.FileName.E ...
- Linux命令总结_文件查找之grep
1.grep命令 grep一般格式为:grep [选项]基本正则表达式[文件]这里基本正则表达式可为字符串,字符串或变量应该用双引号,模式匹配用单引号 选项: -c 只输出匹配行的计数 -i 不区 ...
- 使用Objective-C的文档生成工具:appledoc
使用Objective-C的文档生成工具:appledoc 前言 做项目的人多了,就需要文档了.今天开始尝试写一些项目文档.但是就源代码来说,文档最好和源码在一起,这样更新起来更加方便和顺手.象 Ja ...
- itellyou MSDN, 我告诉你 win7系统工具等
http://www.itellyou.cn/ 操作系统-window7-中文-Windows 7 Ultimate with Service Pack 1 (x64) - DVD (Chinese- ...
- prelaod场景,用来显示资源加载进度
phaser.js的源码可以到它在github上的托管里去下载,游戏要用到的图片声音等素材资源请点击这里下载.Phaser的使用非常简单,只需要引入它的主文件,然后在页面中指定一个用来放置canvas ...
- 【液晶模块系列基础视频】5.4.X-GUI字体驱动4
============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...
- 【新产品发布】【iM_TFTRGB 液晶驱动模块】
============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...
- Java类型相互转换byte[]类型,blob类型
在我们的程序开发当中,经常会用到java.sql.Blob.byte[].InputStream之间的相互转换,但在JDK的API当中,又没有直接给我们提供可用的API,下面的程序片段主要就是实现它们 ...
- 【转】SVN环境搭建教程
http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html http://www.cnblogs.com/xiaobaihom ...