一、用C#自带的StopWatch函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using System.Diagnostics;
 
namespace StopWatch
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            //这里填写要执行的代码
            sw.Stop();
            Console.WriteLine("总运行时间:" + sw.Elapsed);
            Console.WriteLine("测量实例得出的总运行时间(毫秒为单位):" + sw.ElapsedMilliseconds);
            Console.WriteLine("总运行时间(计时器刻度标识):" + sw.ElapsedTicks);
            Console.WriteLine("计时器是否运行:" + sw.IsRunning.ToString());
        }
    }
}

运行结果如下:
总运行时间:00:00:00.0000013
测量实例得出的程序运行时间(毫秒为单位):0
总运行时间(计时器刻度标识):5
计时器是否运行:False

二、用API函数QueryPerformanceFrequency

 1 usingSystem;   
2
3 usingSystem.Threading;
4
5 classClass1
6
7 {
8
9 [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
10
11 static extern bool QueryPerformanceCounter(ref longcount);
12
13 [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
14
15 static extern bool QueryPerformanceFrequency(ref longcount);
16
17 [STAThread]
18
19 static void Main(string[] args)
20
21 {
22
23 longcount = 0;
24
25 longcount1 = 0;
26
27 longfreq = 0;
28
29 doubleresult = 0;
30
31 QueryPerformanceFrequency(reffreq);
32
33 QueryPerformanceCounter(refcount);
34
35 //需要测试的模块
36
37 intheisetoufa;
38
39 for(heisetoufa = 1; heisetoufa < 10000; heisetoufa++)
40
41 {
42
43 Console.WriteLine("第" + heisetoufa + "行");
44
45 if(heisetoufa == 5000)
46
47 {
48
49 Thread.Sleep(10000);
50
51 }
52
53 }
54
55 //需要测试的模块
56
57 QueryPerformanceCounter(refcount1);
58
59 count = count1 - count;
60
61 result = (double)(count) / (double)freq;
62
63 Console.WriteLine("耗时: {0} 秒", result);
64
65 Console.ReadLine();
66
67 }
68
69 }

C#测试程序运行时间的更多相关文章

  1. php测试程序运行时间和占用内存情况

    php测试程序运行时间和占用内存情况: $HeaderTime = microtime(true);//参数true表示返回浮点数值 /** *CODE */ printf(" total ...

  2. C# 测试程序运行时间和cpu使用时间

    方法一 Stopwatch类测试程序运行时间和cpu使用时间 添加命名空间using System.Diagnostics;使用实例如下 private Stopwatch sw = new Stop ...

  3. C#中提供的精准测试程序运行时间的类Stopwatch

    C#中提供的精准测试程序运行时间的类Stopwatch http://www.cnblogs.com/ret00100/archive/2010/08/06/1793680.html 在需要对程序的执 ...

  4. C#测试程序运行时间的方法

    C#测试程序运行时间的三种方法如下: (1)Datetime DateTime dtBegin = System.DateTime.Now;... DateTime dtEnnd = System.D ...

  5. 【VBA】测试程序运行时间,延时方法

    测试程序运行时间 Dim start As Date start = Now() Dim i As Long For i = 0 To 10000000 ' 10 million Next Debug ...

  6. C++中几种测试程序运行时间的方法<转>

    转的地址:https://www.cnblogs.com/silentteen/p/7532855.html 1.GetTickCount()函数 原理: GetTickCount()是获取系统启动后 ...

  7. [C.Sharp] TimeSpan的用法,获取测试程序运行时间

    TimeSpan的用法 TimeSpan是用来表示一个时间段的实例,两个时间的差可以构成一个TimeSpan实例,现在就来简单介绍一下几点重要的用法: a 先来介绍几个方法 TimeSpan.Minu ...

  8. PHP 测试程序运行时间 microtime函数用法

    PHP microtime() 函数PHP Date / Time 函数定义和用法microtime() 函数返回当前 Unix 时间戳和微秒数.语法microtime(get_as_float)参数 ...

  9. c/c++测试程序运行时间

    算法分析中需要对各种算法进行性能测试,下面介绍两种通用的测试方法,由于只用到标准c语言函数,所以在各种平台和编译器下都能使用. 方法1: clock()函数 开始计时:start = clock() ...

随机推荐

  1. vue双向绑定原理

    要了解vue的双向绑定原理,首先得了解Object.defineProperty()方法,因为访问器属性是对象中的一种特殊属性,它不能直接在对象中设置,而必须通过 Object.definePrope ...

  2. js实现2048小游戏

    这是学完javascript基础,编写的入门级web小游戏 游戏规则:在玩法规则也非常的简单,一开始方格内会出现2或者4等这两个小数字,玩家只需要上下左右其中一个方向来移动出现的数字,所有的数字就会想 ...

  3. classNotFound修复

    万恶的这个bug,也是经常遇见,每一次都是在反复检查完代码之后并没有发现错误,然后开始上网百度,发现也并没有一个准确的解答.在此给出我的个人的修复bug的过程. 1.首先我是遇到了找不到entity中 ...

  4. php性能调试工具介绍

    php版本:php7 xhprof: xhprof是php5.*下很好的性能测试工具,配合xhprof_html能够图形显示测试结果,基本够用, 但已经没人维护了. tideways: 在php7下, ...

  5. BigDecimal常用操作

    import java.math.BigDecimal; public class BigDecimalUtil { /** * 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精 ...

  6. dependencyManagement和dependencies的区别

    参考:http://zhaoshijie.iteye.com/blog/2094478http://blog.csdn.net/cpf2016/article/details/45674377 还有一 ...

  7. 转载:document.execCommand()的用法小记

    首先要说明的是在firefox下支持不好.2D-Position 允许通过拖曳移动绝对定位的对象. AbsolutePosition 设定元素的 position 属性为“absolute”(绝对). ...

  8. mysql的mysqladmin的用法

    mysqladmin 适合于linux和windows系统 linux下:mysqladmin -u[username] -p[password] status windows下:先在安装目录找到my ...

  9. hdu 1411(四面体的体积)

    校庆神秘建筑 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  10. catkin详细解释

    http://answers.ros.org/question/58498/what-is-the-purpose-of-catkin_depends/