|
using System;
using System.Threading;
namespace QiHe.CodeLib
{
/// <summary>
/// Timer ��ժҪ˵����
/// </summary>
public class LiteTimer
{
static long begin;
static long end;
/// <summary>
/// The number of milliseconds elapsed from Start().
/// </summary>
public static int Duration
{
get
{
return (int)((end - begin) / 10000);
}
}
public static string ShowCurrentTime()
{
string month, day, dayofweek, hour, minute, second, millisecond;
month = DateTime.Now.Month.ToString() + "��";
day = DateTime.Now.Day.ToString() + "��";
dayofweek = DateTime.Now.DayOfWeek.ToString();
hour = DateTime.Now.Hour.ToString() + ":";
minute = DateTime.Now.Minute.ToString("d2");
second = DateTime.Now.Second.ToString();
millisecond = DateTime.Now.Millisecond.ToString();
return month + day + " " + dayofweek + " " + hour + minute;
}
public static void Start()
{
begin = DateTime.Now.Ticks;
}
public static void Stop()
{
end = DateTime.Now.Ticks;
}
public static string ShowTimeSpan()
{
TimeSpan ts = new TimeSpan(end - begin);
return ShowTimeSpan(ts);
}
public static string ShowTimeSpan(TimeSpan ts)
{
string time = "";
if (ts.Hours > 0) time += ts.Hours + "Сʱ";
if (ts.Minutes > 0) time += ts.Minutes + "��";
time += ts.Seconds + "��";
time += ts.Milliseconds + "����";
return time;
}
}
/// <summary>
/// High-Resolution Performance Timer
/// </summary>
public class HighTimer
{
// The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter.
// receives the current performance-counter value, in counts.
// If the function succeeds, the return value is nonzero.
// If the function fails, the return value is zero.
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceCounter(out long freq);
//The QueryPerformanceFrequency function retrieves the frequency of
//the high-resolution performance counter in counts per second, if one exists.
//The frequency cannot change while the system is running.
//If the installed hardware supports a high-resolution performance counter, the return value is nonzero.
//If the function fails, the return value is zero.
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
extern static bool QueryPerformanceFrequency(out long count);
private long startTime, stopTime;
private long freq;
// Constructor
public HighTimer()
{
startTime = 0;
stopTime = 0;
if (QueryPerformanceFrequency(out freq) == false)
{
//throw new Exception("high-performance counter is not supported");
throw new System.ComponentModel.Win32Exception();
}
}
// Start the timer
public void Start()
{
// lets do the waiting threads there work
Thread.Sleep(0);
QueryPerformanceCounter(out startTime);
}
// Stop the timer
public void Stop()
{
QueryPerformanceCounter(out stopTime);
}
/// <summary>
/// Returns the duration of the timer (in seconds)
/// </summary>
public double Duration
{
get
{
return (double)(stopTime - startTime) / (double)freq;
}
}
/// <summary>
/// Returns the elapsed time of the timer (in seconds)
/// without call Stop(), for use in split timing
/// </summary>
public double Elapsed
{
get
{
QueryPerformanceCounter(out stopTime);
return (double)(stopTime - startTime) / (double)freq;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.