//【直接上可以运行的代码文件】
//定时.cpp
//取时间0、GetLocalTime、GetSystemTime
#include <afxcoll.h>//包含函数WinExec、Sleep,类型UINT
#include <mmsystem.h>
void main()
{
char *weekDay[] ={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char *daylight[]={"白天","黑夜"};
SYSTEMTIME sys;
GetLocalTime(&sys);
printf(" 本地计算机(标准)时间=%d/%d/%d %s %02d:%02d:%02d.%03d\n",sys.wYear,sys.wMonth,sys.wDay,weekDay[sys.wDayOfWeek],sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds);
GetSystemTime(&sys);
printf("本地计算机(标准)时间对应的UTC时间=%d/%d/%d %s %02d:%02d:%02d.%03d\n",sys.wYear,sys.wMonth,sys.wDay,weekDay[sys.wDayOfWeek],sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds);
//UTC时间+时区差=本地标准时间,东时区的时区差为正数,西时区的时区差为负数,北京时间为东八区时区差为+8
struct tm *tp;
time_t curTime;
time( &curTime ); //取得long integer的当地时间
tp = localtime( &curTime ); //转换成tm格式的当地时间
printf("当前时间为:%d.%d.%d %d:%d:%d\n",(1900+tp->tm_year), (1+tp->tm_mon), tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
printf("今天为当年第%d天, %s, 当前为:%s\n",tp->tm_yday+1,weekDay[tp->tm_wday],daylight[tp->tm_isdst]);
}
/*
//timeGetTime需要以下3句
#include <afxcoll.h>//包含函数WinExec、Sleep,类型UINT
#include <windows.h>
#include <stdio.h>
#include <mmsystem.h>
#pragma comment( lib, "winmm.lib" )
void main()
{
printf("----------------------------------1----------time(NULL)、localtime计时\n");
//1、time(NULL)、localtime
long beginTime,endTime,lastTime;
beginTime=time(NULL); //系统当前时间距离计时起点(一般是1970年1月1日0时0分0秒)的时间(s)----计时起点值
Sleep(1000);
endTime=time(NULL); //系统当前时间距离计时起点(一般是1970年1月1日0时0分0秒)的时间(s)----计时终点值
lastTime=endTime-beginTime;
printf("计时起点值=%d(s)\n",beginTime);
printf("计时终点值=%d(s)\n",endTime);
printf("间隔时间=%d(s)\n",lastTime);
//获取结构体时间
char *weekDay[] ={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char *daylight[]={"白天","黑夜"};
struct tm *p;
p = localtime(&beginTime); //获取结构体时间----->显然,localtime需要time配套才能使用
printf("计时起点:%d.%d.%d %d:%d:%d %s ",(1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec,weekDay[p->tm_wday]);
printf("当年第%d天 %s\n",p->tm_yday+1,daylight[p->tm_isdst]);
p = localtime(&endTime); //获取结构体时间----->显然,localtime需要time配套才能使用
printf("计时终点:%d.%d.%d %d:%d:%d %s ",(1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec,weekDay[p->tm_wday]);
printf("当年第%d天 %s\n",p->tm_yday+1,daylight[p->tm_isdst]);
printf("\n");
printf("----------------------------------2----------time(long *)、localtime计时\n");
//2、time(long *)、localtime
//long beginTime,endTime,lastTime;
time(&beginTime);//系统当前时间距离计时起点(一般是1970年1月1日0时0分0秒)的时间(s)----计时起点值
Sleep(1000);
time(&endTime); //系统当前时间距离计时起点(一般是1970年1月1日0时0分0秒)的时间(s)----计时终点值
lastTime=endTime-beginTime;
printf("计时起点值=%d(s)\n",beginTime);
printf("计时终点值=%d(s)\n",endTime);
printf("间隔时间=%d(s)\n",lastTime);
//获取结构体时间
//char *weekDay[] ={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
//char *daylight[]={"白天","黑夜"};
//struct tm *p;
p = localtime(&beginTime); //获取结构体时间----->显然,localtime需要time配套才能使用
printf("计时起点:%d.%d.%d %d:%d:%d %s ",(1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec,weekDay[p->tm_wday]);
printf("当年第%d天 %s\n",p->tm_yday+1,daylight[p->tm_isdst]);
p = localtime(&endTime); //获取结构体时间----->显然,localtime需要time配套才能使用
printf("计时终点:%d.%d.%d %d:%d:%d %s ",(1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec,weekDay[p->tm_wday]);
printf("当年第%d天 %s\n",p->tm_yday+1,daylight[p->tm_isdst]);
printf("\n");
printf("----------------------------------3----------clock计时\n");
//long beginTime,endTime,lastTime;
beginTime=clock();//本程序启动后经历的时间间隔(ms)----计时起点值
Sleep(1000);
endTime=clock(); //本程序启动后经历的时间间隔(ms)----计时终点值
lastTime=endTime-beginTime;
printf("计时终点值=%d(ms)\n",endTime);
printf("计时起点值=%d(ms)\n",beginTime);
printf("间隔时间=%d(ms)\n",lastTime);
printf("\n");
printf("----------------------------------4----------GetTickCount计时\n");
DWORD beginTimeu,endTimeu,lastTimeu;
beginTimeu=GetTickCount();//计算机启动后经历的时间间隔(ms)----计时起点值
Sleep(1000);
endTimeu=GetTickCount(); //计算机启动后经历的时间间隔(ms)----计时终点值
lastTimeu=endTimeu-beginTimeu;
printf("计时起点值=%d(ms)\n",beginTimeu);
printf("计时终点值=%d(ms)\n",endTimeu);
printf("间隔时间=%d(ms)\n",lastTimeu);
printf("\n");
printf("----------------------------------5----------GetCurrentTime计时\n");
//DWORD beginTimeu,endTimeu,lastTimeu;
beginTimeu=GetCurrentTime();//计算机启动后经历的时间间隔(ms)----计时起点值
Sleep(1000);
endTimeu=GetCurrentTime(); //计算机启动后经历的时间间隔(ms)----计时终点值
lastTimeu=endTimeu-beginTimeu;
printf("计时起点值=%d(ms)\n",beginTimeu);
printf("计时终点值=%d(ms)\n",endTimeu);
printf("间隔时间=%d(ms)\n",lastTimeu);
printf("\n");
printf("----------------------------------6----------timeGetTime计时\n");
//DWORD beginTime,endTime,lastTime;
beginTimeu=timeGetTime();//Windows启动后经历的时间间隔(ms)----计时起点值
Sleep(1000);
endTimeu=timeGetTime(); //Windows启动后经历的时间间隔(ms)----计时终点值
lastTimeu=endTimeu-beginTimeu;
printf("计时起点值=%u(ms)\n",beginTimeu);
printf("计时终点值=%u(ms)\n",endTimeu);
printf("间隔时间=%u(ms)\n",lastTimeu);
}
*/
/*
//定时7、QueryPerformanceCounter
#include <windows.h>
#include <stdio.h>
void main()
{
LARGE_INTEGER litmp0,litmp1,litmp2;//lastTime;
QueryPerformanceFrequency(&litmp0);//本机内部定时器的时钟频率
long cpuFreq=litmp0.QuadPart;
printf("本机内部定时器的时钟频率=%u\n", cpuFreq);
QueryPerformanceCounter(&litmp1);
Sleep(1000);
QueryPerformanceCounter(&litmp2);
double beginTime,endTime,lastTime;
beginTime=1000*litmp1.QuadPart/cpuFreq;
endTime=1000*litmp2.QuadPart/cpuFreq;
lastTime=endTime-beginTime;
printf("计时起点值=%f(ms)\n",beginTime);
printf("计时终点值=%f(ms)\n",endTime);
printf("间隔时间=%f(ms)\n",lastTime);
}
*/
/*
//定时8、rdtsc指令定时1
#include <stdio.h>
#include <afxcoll.h>//包含函数WinExec、Sleep,类型UINT
inline unsigned __int64 GetCycleCount(void)
//获得开机后CPU到现在的运行周期数
{
//_asm _emit 0x0F
//_asm _emit 0x31
_asm rdtsc; //Read Time-Stamp Counter等价于上两句【汇编指令】
}
#include <stdio.h>
#define testTime0 1000
void main()
{
double lastTime;//ms
unsigned __int64 beginCycleCount=GetCycleCount();//获得开机后CPU到现在的运行周期数
Sleep(testTime0);
unsigned __int64 endCycleCount=GetCycleCount();
signed __int64 cpuFrequency=endCycleCount-beginCycleCount;
printf("CPU主频=%I64u\n",cpuFrequency);
signed __int64 lastCycleCount;
beginCycleCount=GetCycleCount();//获得开机后CPU到现在的运行周期数
Sleep(1001);
endCycleCount=GetCycleCount();
lastCycleCount=endCycleCount-beginCycleCount;
lastTime=lastCycleCount/(double)cpuFrequency*1000;
printf("间隔时间=%.15f(ms)\n",lastTime);
}
*/
/*
//定时9、rdtsc指令定时2
#include <stdio.h>
#include <afxcoll.h>//包含函数WinExec、Sleep,类型UINT
inline unsigned __int64 GetCycleCount(void)
//获得开机后CPU到现在的运行周期数
{
//_asm _emit 0x0F
//_asm _emit 0x31
_asm rdtsc; //Read Time-Stamp Counter等价于上两句【汇编指令】
}
class KTimer
{
unsigned __int64 startcycle;
public:
unsigned __int64 overhead;
KTimer(void)
{
overhead = 0;
Start();
overhead = Stop();
}
unsigned __int64 Start(void)
{
startcycle = GetCycleCount();
return startcycle;
}
unsigned __int64 Stop(void)
{
return GetCycleCount()-startcycle-overhead;
}
};
#include <stdio.h>
main()
{
double lastTime;//ms
unsigned __int64 beginCycleCount=GetCycleCount();//获得开机后CPU到现在的运行周期数
Sleep(1000);
unsigned __int64 endCycleCount=GetCycleCount();
signed __int64 cpuFrequency=endCycleCount-beginCycleCount;
printf("CPU主频=%I64u\n",cpuFrequency);
signed __int64 lastCycleCount;
KTimer timer;
timer.Start();
Sleep(5);
lastCycleCount=timer.Stop();
lastTime=lastCycleCount/(double)cpuFrequency*1000;
printf("间隔时间=%.15f(ms)\n",(double)lastTime);
}
*/
//定时10:COM对象中的COleDateTime,COleDateTimeSpan类
//定时11:MFC下的事件SetTimer
//定时12:windows下的timeSetEvent
//最后三个在控制台程序中无法实现