Files
2026-07-10 15:25:05 +08:00

77 lines
2.0 KiB
C#

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace Zhaizj.Framework {
/// <summary>
/// 秒表,用于测试程序耗时
/// </summary>
public class Stopwatch {
private Boolean _IsStop;
private long frequency;
private decimal multiplier = 1000000000M;
private long start;
private long stop;
[DllImport( "KERNEL32" )]
private static extern Boolean QueryPerformanceCounter( out long lpPerformanceCount );
[DllImport( "Kernel32.dll" )]
private static extern Boolean QueryPerformanceFrequency( out long lpFrequency );
public Stopwatch() {
if (!QueryPerformanceFrequency( out this.frequency )) {
throw new Win32Exception();
}
}
/// <summary>
/// 开始启动
/// </summary>
public void Start() {
this._IsStop = false;
QueryPerformanceCounter( out this.start );
}
/// <summary>
/// 停止
/// </summary>
public void Stop() {
if (!this._IsStop) {
QueryPerformanceCounter( out this.stop );
this._IsStop = true;
}
}
/// <summary>
/// 总共耗时(毫秒)
/// </summary>
public double ElapsedMilliseconds {
get {
double result = this.getDuration() / 1000000.0;
if (result < 0) result = 0;
return result;
}
}
/// <summary>
/// 总共耗时(秒)
/// </summary>
public double ElapsedSeconds {
get {
double result = this.ElapsedMilliseconds / 1000.0;
if (result < 0) result = 0;
return result;
}
}
private double getDuration() {
return (((this.stop - this.start) * ((double)this.multiplier)) / ((double)this.frequency));
}
}
}