123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Runtime.InteropServices;
- namespace ServiceHelper
- {
- public class QueryPerformance
- {
- [DllImport("Kernel32.dll")]
- private static extern bool QueryPerformanceCounter(out long performanceCount);
- [DllImport("Kernel32.dll")]
- private static extern bool QueryPerformanceFrequency(out long frequency);
- private long begintTime = 0;//开始时间
- private long endTime = 0;//结束时间
- private long frequency = 0;//处理器频率
- public long BegintTime
- {
- get { return begintTime; }
- }
- public long EndTime
- {
- get { return endTime; }
- }
- public long Frequency
- {
- get { return frequency; }
- }
- public QueryPerformance()
- {
- QueryPerformanceFrequency(out frequency);//获取频率
- }
- public void Start()
- {
- QueryPerformanceCounter(out begintTime);
- }
- public string Stop(bool showRecord)
- {
- QueryPerformanceCounter(out endTime);
- if (showRecord)
- {
- return string.Format("用时:{0}ms", TastTime);
- }
- return null;
- }
- public double TastTime//花费时间:单位S
- {
- get
- {
- if (frequency > 0)
- return (double)(endTime - begintTime)*1000 / frequency;
- else
- return 0;
- }
- }
- }
- }
|