c# 计算程序执行时间,计算一段代码执行所用的时间,测试效率

  1. using System;   
  2. using System.Threading;   
  3. class Class1   
  4. {   
  5.     [System.Runtime.InteropServices.DllImport("Kernel32.dll")]   
  6.     static extern bool QueryPerformanceCounter(ref long count);   
  7.     [System.Runtime.InteropServices.DllImport("Kernel32.dll")]   
  8.     static extern bool QueryPerformanceFrequency(ref long count);   
  9.     [STAThread]   
  10.     static void Main(string[] args)   
  11.     {   
  12.         long count = 0;   
  13.         long count1 = 0;   
  14.         long freq = 0;   
  15.         double result = 0;   
  16.         QueryPerformanceFrequency(ref freq);   
  17.         QueryPerformanceCounter(ref count);   
  18.         //需要测试的模块   
  19.   
  20.         int heisetoufa;   
  21.         for (heisetoufa = 1; heisetoufa < 10000; heisetoufa++)   
  22.         {   
  23.             Console.WriteLine("第" + heisetoufa + "行");   
  24.             if (heisetoufa == 5000)   
  25.             {   
  26.                 Thread.Sleep(10000);   
  27.             }   
  28.         }   
  29.   
  30.         //需要测试的模块   
  31.   
  32.         QueryPerformanceCounter(ref count1);   
  33.         count = count1 - count;   
  34.         result = (double)(count) / (double)freq;   
  35.         Console.WriteLine("耗时: {0} 秒", result);   
  36.         Console.ReadLine();   
  37.     }   
  38. }  

转载于:https://www.cnblogs.com/jordan2009/archive/2009/08/25/1553615.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值