输出函数调用时间 多少毫秒(转)
/// <summary>
/// 得到系统时钟周期的当前值
/// </summary>
/// <param name="lpPerformanceCount">输出参数,得到系统时钟周期的当前值</param>
/// <returns>返回是否获取成功</returns>
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
/// <summary>
/// 得到系统的时钟频率,每秒的周期数
/// </summary>
/// <param name="frequency">输出参数,得到系统的每秒周期数</param>
/// <returns>返回是否获取成功</returns>
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool QueryPerformanceFrequency(out long frequency);
/// 得到系统时钟周期的当前值
/// </summary>
/// <param name="lpPerformanceCount">输出参数,得到系统时钟周期的当前值</param>
/// <returns>返回是否获取成功</returns>
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
/// <summary>
/// 得到系统的时钟频率,每秒的周期数
/// </summary>
/// <param name="frequency">输出参数,得到系统的每秒周期数</param>
/// <returns>返回是否获取成功</returns>
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool QueryPerformanceFrequency(out long frequency);
调用:
long start, end; //时钟周期
long frequency; //时钟频率
DateTimeEx.QueryPerformanceFrequency(out frequency);
DateTimeEx.QueryPerformanceCounter(out start);
CallFunction();
DateTimeEx.QueryPerformanceCounter(out end);
long frequency; //时钟频率
DateTimeEx.QueryPerformanceFrequency(out frequency);
DateTimeEx.QueryPerformanceCounter(out start);
CallFunction();
DateTimeEx.QueryPerformanceCounter(out end);
Console.WriteLine("CallFunction函数运行用时: " + ((end - start) * 1000 / frequency).ToString());
检测调用函数用了多长时间的