精确计算代码执行时间
2008-09-10 09:24
519 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace cxb.RunTime
{
/// <summary>
/// 精确计算代码执行时间
/// </summary>
class RunTimeComputer
{
private static long start = 0;
private static long end = 0;
private static long freq = 0;
private static double result = 0;
[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);
/// <summary>
/// 测试模块开始
/// </summary>
public static void Start()
{
start = 0;
freq = 0;
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref start);
}
/// <summary>
/// 测试模块结束
/// </summary>
public static void End()
{
end = 0;
QueryPerformanceCounter(ref end);
}
/// <summary>
/// 获得执行结果,返回运行时间
/// </summary>
/// <returns></returns>
public static double GetRunTime()
{
start = end - start;
result = (double)(start) / (double)freq;
return result;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace cxb.RunTime
{
/// <summary>
/// 精确计算代码执行时间
/// </summary>
class RunTimeComputer
{
private static long start = 0;
private static long end = 0;
private static long freq = 0;
private static double result = 0;
[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);
/// <summary>
/// 测试模块开始
/// </summary>
public static void Start()
{
start = 0;
freq = 0;
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref start);
}
/// <summary>
/// 测试模块结束
/// </summary>
public static void End()
{
end = 0;
QueryPerformanceCounter(ref end);
}
/// <summary>
/// 获得执行结果,返回运行时间
/// </summary>
/// <returns></returns>
public static double GetRunTime()
{
start = end - start;
result = (double)(start) / (double)freq;
return result;
}
}
}
相关文章推荐
- VC精确计算代码执行时间方法
- C++精确计算代码执行时间精度微秒级
- 计算php代码执行时间长短的类(精确到毫秒)
- VC精确计算代码执行时间
- 精确计算代码执行时间
- C++精确计算代码执行时间精度微秒级
- 精确计算代码执行时间
- 精确计算代码执行时间
- 精确计算代码执行时间
- 精确计算代码执行时间
- c++怎么计算代码运行时间到超精确毫秒级别
- Java、Android超精确测量代码执行时间差
- 获取C#代码执行的时间(精确到毫秒)
- java计算一段代码执行的时间
- js 页面执行时间计算代码
- 分享一个PHP代码的执行时间,精确到微秒级
- C++中计算代码的执行时间
- 计算执行的时间的代码
- 获取C#代码执行的时间(精确到毫秒)
- C#计算代码执行时间的方法