c# 计算程序执行时间,计算一段代码执行所用的时间,测试效率
2009-08-25 14:18
986 查看
using System;
using System.Threading;
class Class1
{
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceCounter(ref long count);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceFrequency(ref long count);
[STAThread]
static void Main(string[] args)
{
long count = 0;
long count1 = 0;
long freq = 0;
double result = 0;
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
//需要测试的模块
int heisetoufa;
for (heisetoufa = 1; heisetoufa < 10000; heisetoufa++)
{
Console.WriteLine("第" + heisetoufa + "行");
if (heisetoufa == 5000)
{
Thread.Sleep(10000);
}
}
//需要测试的模块
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("耗时: {0} 秒", result);
Console.ReadLine();
}
}
using System.Threading;
class Class1
{
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceCounter(ref long count);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceFrequency(ref long count);
[STAThread]
static void Main(string[] args)
{
long count = 0;
long count1 = 0;
long freq = 0;
double result = 0;
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
//需要测试的模块
int heisetoufa;
for (heisetoufa = 1; heisetoufa < 10000; heisetoufa++)
{
Console.WriteLine("第" + heisetoufa + "行");
if (heisetoufa == 5000)
{
Thread.Sleep(10000);
}
}
//需要测试的模块
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("耗时: {0} 秒", result);
Console.ReadLine();
}
}
相关文章推荐
- MFC怎样测试一段代码执行所用时间?
- C# 计算一段代码执行的时间函数
- 测试一段程序代码执行时间的方法
- 测试一段程序代码执行时间的方法
- 计算一段程序的执行时间
- C++测试程序执行时间代码
- C#计算程序执行的时间间隔
- C#计算一段代码的运行时间
- C#计算程序执行过程花费时间的方法
- C#计算一段程序的运行时间
- C# 程序执行时间查看, 效率查看
- Python:计算一段代码的执行时间
- C#计算一段程序运行时间的三种方法
- C#计算一段程序运行时间的三种方法
- c# 计算程序执行时间
- 通过计算python执行时间,来提升程序的运行效率
- C#计算一段程序运行时间的三种方法
- java计算一段代码执行的时间
- 测试一段C代码的执行时间(windows系统和ubuntu系统)
- 用GetTickCount()计算一段代码执行耗费的时间的小例子