三种计算c#程序运行时间的方法

第一种

利用 **System.DateTime.Now **

// example1: System.DateTime.Now method
DateTime dt1 = System.DateTime.Now;
System.Threading.Thread.Sleep(time_cap);
DateTime dt2 = System.DateTime.Now; TimeSpan ts = dt2.Subtract(dt1);
Console.WriteLine("example1 time {0}", ts.TotalMilliseconds);

第二种

利用Stopwatch

// example2: Stopwatch class
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
System.Threading.Thread.Sleep(time_cap);
sw.Stop(); TimeSpan ts2 = sw.Elapsed;
Console.WriteLine("example2 time {0}", ts2.TotalMilliseconds);

第三种

利用c++ API

// example3: c++ API
long count1 = 0;
long count2 = 0;
long freq = 0;
double result = 0; QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count1);
System.Threading.Thread.Sleep(time_cap);
QueryPerformanceCounter(ref count2); result = (double)(count2 - count1) / (double)freq;
Console.WriteLine("example3 time {0}", result);

测试结果:

example1 time 2001.486

example2 time 2000.6587

example3 time 2.00032488719712

二三两种方法结果比较准确

完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication
{
class Program
{
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceFrequency(ref long count); [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceCounter(ref long count); static void Main(string[] args)
{
int time_cap = 2000; // example1: System.DateTime.Now method
DateTime dt1 = System.DateTime.Now;
System.Threading.Thread.Sleep(time_cap);
DateTime dt2 = System.DateTime.Now; TimeSpan ts = dt2.Subtract(dt1);
Console.WriteLine("example1 time {0}", ts.TotalMilliseconds); // example2: Stopwatch class
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
System.Threading.Thread.Sleep(time_cap);
sw.Stop(); TimeSpan ts2 = sw.Elapsed;
Console.WriteLine("example2 time {0}", ts2.TotalMilliseconds); // example3: c++ API
long count1 = 0;
long count2 = 0;
long freq = 0;
double result = 0; QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count1);
System.Threading.Thread.Sleep(time_cap);
QueryPerformanceCounter(ref count2); result = (double)(count2 - count1) / (double)freq;
Console.WriteLine("example3 time {0}", result); Console.ReadLine();
}
}
}

三种计算c#程序运行时间的方法的更多相关文章

  1. 两种计算Java对象大小的方法

    之前想研究一下unsafe类,碰巧在网上看到了这篇文章,觉得写得很好,就转载过来.原文出处是: http://blog.csdn.net/iter_zc/article/details/4182271 ...

  2. [转]SQL三种获取自增长的ID方法

     最新更新请访问: http://denghejun.github.io   SQL SERVER中的三种获得自增长ID的方法  这个功能比较常用,所以记下来以防自己忘掉. SCOPE_IDENTIT ...

  3. 三种dedecms调用相关文章的方法

    在文章的末尾或侧边栏添加相关文章可以提高用户的黏度,提高pv,增加se的好印象(哈哈),那么dedecms如何调用相关文章呢?有三种方法可以实现. 第一种dedecms调用相关文章的方法,用默认的li ...

  4. MySQL、SQLServer2000(及SQLServer2005)和ORCALE三种数据库实现分页查询的方法

    在这里主要讲解一下MySQL.SQLServer2000(及SQLServer2005)和ORCALE三种数据库实现分页查询的方法. 可能会有人说这些网上都有,但我的主要目的是把这些知识通过我实际的应 ...

  5. 三种将list转换为map的方法(传统方法、jdk8 Stream流、guava)

    三种将list转换为map的方法 - jackyrong - ITeye博客:http://jackyrong.iteye.com/blog/2158009

  6. 对tableView三种计算动态行高方法的分析

    tableView是一个神奇的东西,可以这么说,就算是一个初学者如果能把tableView玩的很6,那编一般的iOS的需求都问题不大了.tableView是日常开发中用烂了的控件,但是关于tableV ...

  7. 旧书重温:0day2【2】 实验:三种获取kernel32.dll基址的方法

    0x01 找kernel32基地址的方法一般有三种: 暴力搜索法.异常处理链表搜索法.PEB法. 0x02 基本原理 暴力搜索法是最早的动态查找kernel32基地址的方法.它的原理是几乎所有的win ...

  8. Win10计算器在哪里?三种可以打开Win10计算器的方法图文介绍

    全新的windows10系统带来了不少新的特性和改变,其中win10的计算器位置就发生了很多的变化,导致很多网友们都以为win10计算器不见了,那么,win10计算器在哪里?如何打开?针对此问题,本文 ...

  9. 三种实现PHP伪静态页面的方法(转)

    PHP伪静态写法--其一 伪静态又名:URL重写 以下列举了三种方法. 方法一: 比如这个网页 http://www.2cto.com /soft.php/1,100,8630.html 其实处理的脚 ...

随机推荐

  1. NYOJ--353--bfs+优先队列--3D dungeon

    /* Name: NYOJ--3533D dungeon Author: shen_渊 Date: 15/04/17 15:10 Description: bfs()+优先队列,队列也能做,需要开一个 ...

  2. 关于个人编辑器sublime text3使用指南

    用过了好多编辑器,前些年用的zend studio,phpstorm近两年转为nodepad++(因为写的语言种类比较多了,shell,python,php,前端等),相对于nodepad++,  s ...

  3. HTML颜色代码表/颜色名(网摘)

    HTML颜色代码表 原文出处:[颜色代码] HTML颜色名 source link: http://www.runoob.com/html/html-colornames.html

  4. Get host name and port(Object-c)

    /************************************************************************** @param pChHostName: [out ...

  5. Python 搭建环境踩过的那些坑

    实践出真知,学习 Python 第六天,准备开始尝试写一些 Python 的实例.之前学习过程主要是参照的 廖雪峰老师的教程.搭建环境的过程,被很多坑围绕着. 版本选择 版本选择 Python 3.5 ...

  6. java8之stream

    lambda表达式是stream的基础,初学者建议先学习lambda表达式,http://www.cnblogs.com/andywithu/p/7357069.html 1.初识stream 先来一 ...

  7. android_orm框架之greenDAO(二)

    一.概述 在上次greenDao第一篇文章中,我们对greenDao的使用步骤和基本用法给大家做了介绍,文章链接:http://www.cnblogs.com/jerehedu/p/4304766.h ...

  8. js基础语法习题补充

    输入三个整数,x,y,z,最终以从小到大的方式输出. <<script> var x=prompt("请输入数字") var y=prompt("请输入 ...

  9. GoF设计模式 - 概述

    掌握编程语言仅仅意味着掌握了如何给计算机"下命令",而到底要计算机如何去做,怎么指挥,则是另一个问题--如何编程.设计模式是一套程序员的"武功套路",它教我们如 ...

  10. java多线程基础(synchronize关键字)

    [toc] 基础知识 ---- 线程:进程(process)就是一块包含了某些资源的内存区域.操作系统利用进程把它的工作划分为一些功能单元. 线程:进程中所包含的一个或多个执行单元称为线程(threa ...