数据结构和算法 – 番外篇.时间测试类Timing
2016-04-28 14:34
369 查看
public class Timing { //startingTime--用来存储正在测试的代码的开始时间。 TimeSpan startingTime; //duration——用来存储正在测试的代码的终止时间。 TimeSpan durantion; public Timing() { startingTime = new TimeSpan(0); durantion = new TimeSpan(0); } public void startTime() { //先强制对所有代码进行回收 GC.Collect(); //挂起当前线程,直到处理终结器队列的线程清空该队列为止 GC.WaitForPendingFinalizers(); //获取关联程序运行代码所用的时间 startingTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime; } public void StopTime() { durantion = Process.GetCurrentProcess().Threads[0]. UserProcessorTime.Subtract(startingTime); } public TimeSpan Result() { return durantion; } }
测试
static void Main(string[] args) { //DateTime starttime = DateTime.Now; //Print(10000); ////PrintN(100000); //DateTime endtime = DateTime.Now; //double end = TimeHelp.Service.Timehelp(starttime, endtime); //Console.WriteLine("耗时:" + end); Timing tObj = new Timing(); tObj.startTime(); Print(500000); tObj.StopTime(); Console.WriteLine("耗时:" + tObj.Result().TotalSeconds); Console.Read(); } public static void Print(int N) { for (int i = 0; i <= N; i++) { Console.WriteLine(i); } return; }
相关文章推荐
- 数据结构之数组篇
- 链表简述
- 【数据结构】之二叉树的java实现
- 数据结构之位运算篇
- 数据结构与算法目录
- 数据结构与算法系列----学习数据结构与算法前你需要知道的
- 数据结构--树的遍历(前序)
- 【数据结构与算法】链表问题集锦
- Python Cookbook - 1 - 数据结构和算法
- 数据结构实验:将十进制数转成其它进制,C语言实现
- poj--2823 Sliding Window(单调队列)
- 几个简单的排序算法
- 循环链表
- redis之数据结构--上
- linux内核源码分析(内存管理)--之数据结构
- 数据结构-队列的链式存储(链队列)
- 【线段树】浅谈区间问题(1)
- 第五章 Python数据结构
- 数据结构 - 线性表的顺序实现
- 线索化二叉树