110,533
社区成员
发帖
与我相关
我的任务
分享
[DllImport("kernel32")]
static extern uint GetTickCount();
uint s1 = GetTickCount();
Thread.Sleep(2719);
Console.WriteLine(GetTickCount() - s1); //单位毫秒
[DllImport("winmm")]
static extern uint timeGetTime();
[DllImport("winmm")]
static extern void timeBeginPeriod(int t);
[DllImport("winmm")]
static extern void timeEndPeriod(int t);
timeBeginPeriod(1);
uint start = timeGetTime();
Thread.Sleep(2719);
Console.WriteLine(timeGetTime() - start); //单位毫秒
timeEndPeriod(1);
int aa = System.Environment.TickCount;
Thread.Sleep(2719);
Console.WriteLine(System.Environment.TickCount - aa); //单位毫秒
[DllImport("kernel32.dll ")]
static extern bool QueryPerformanceCounter(ref long lpPerformanceCount);
[DllImport("kernel32")]
static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);
long a = 0;
QueryPerformanceFrequency(ref a);
long b = 0, c = 0;
QueryPerformanceCounter(ref b);
Thread.Sleep(2719); QueryPerformanceCounter(ref c);
Console.WriteLine((c - b) / (decimal)a); //单位秒
// MLTimerDot.h
#pragma once
using namespace System;
namespace MLTimerDot {
//得到计算机启动到现在的时钟周期
unsigned __int64 GetCycleCount(void)
{
_asm _emit 0x0F
_asm _emit 0x31
}
//声明 .NET 类
public __gc class MLTimer
{
public:
MLTimer(void)
{
}
//计算时钟周期
UInt64 GetCount(void)
{
return GetCycleCount();
}
};
}
long a = 0;
QueryPerformanceFrequency(ref a);
MLTimerDot.MLTimer timer = new MLTimerDot.MLTimer();
ulong ss= timer.GetCount();
Thread.Sleep(2719);
Console.WriteLine((timer.GetCount() - ss) / (decimal)a);