zoukankan      html  css  js  c++  java
  • 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. }  
  • 相关阅读:
    软件编写和设计中的18大原则
    Ubuntu CTRL+ALT+F1~F6 进入命令模式后不支持中文显示的解决办法
    BM串匹配算法
    KMP串匹配算法解析与优化
    mongodb随机查询一条记录的正确方法!
    这真的该用try-catch吗?
    计算机的本质与数值、文字、声音、图像
    编程语言的概念
    linux服务方式启动程序脚本(init.d脚本)
    linux的7种运行级别
  • 原文地址:https://www.cnblogs.com/jordan2009/p/1553615.html
Copyright © 2011-2022 走看看