zoukankan      html  css  js  c++  java
  • VC++获得当前系统时间的几种方案

    //方案- 优点:仅使用C标准库;缺点:只能精确到秒级

    1. #include < time.h>   
    2. #include < stdio.h>   
    3. int main( void )   
    4. {   
    5. time_t t = time( 0 );   
    6. char tmp[64];   
    7. strftime( tmp, sizeof(tmp), " %Y/%m/%d %X %A 本年第%j天 %z" , localtime(&t) );   
    8. puts( tmp );   
    9. return 0;   
    10. }  

    //方案二 优点:能精确到毫秒级;缺点:使用了windows API

    1. #include < windows.h>   
    2. #include < stdio.h>   
    3. int main( void )   
    4. {   
    5. SYSTEMTIME sys;   
    6. GetLocalTime( &sys );   
    7. printf( " %4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d "   
    8. ,sys.wYear,sys.wMonth,sys.wDay   
    9. ,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds   
    10. ,sys.wDayOfWeek);   
    11. return 0;   
    12. }  

    //方案三,优点:利用系统函数

    1. #include< stdlib.h>   
    2. #include< iostream>   
    3. using namespace std;   
    4. void main(){   
    5. system(" time" );   
    6. }  

    可以改变电脑的时间设定

    方案4:

    1. #include< iostream>   
    2. #include< ctime>   
    3. using namespace std;   
    4. int main()   
    5. {   
    6. time_t now_time;   
    7. now_time = time(NULL);   
    8. cout< < now_time;   
    9. return 0;   
    10. }  

    另一:_strdate(tempstr);

    另二: CTime

    1. CString CTestView::GetTime()   
    2. {   
    3. CTime CurrentTime=CTime::GetCurrentTime();   
    4. CString strTime;   
    5. strTime.Format(" %d:%d:%d" ,CurrentTime.GetHour(), CurrentTime.GetMinute()   
    6. ,CurrentTime.GetSecond());   
    7. return strTime;   
    8. windows,C++获取微秒级时间 

      对于不同算法之间的效率,用毫秒已经不能满足要求了。下面我们就用微秒来比较。
      int fun()
      {
      ……
      } int main()
      {
      LARGE_INTEGER lSatrt,lEnd,lFeq;
      QueryPerformanceFrequency(&lFeq);//每秒跳动次数
      QueryPerformanceCounter(&lSatrt);//测前跳动次数
      fun();
      QueryPerformanceCounter(&lEnd);//测后跳动次数
      double dP=((double)lSatrt.QuadPart-(double)lEnd.QuadPart)*1000000/(double)lFeq.QuadPart;//时间微秒
      char szTmp[16]={0};
      sprintf(szTmp,"time=%.1f ",dP);
      OutputDebugString(szTmp);
      }
       

  • 相关阅读:
    HDU 1058 Humble Numbers
    HDU 1421 搬寝室
    HDU 1176 免费馅饼
    七种排序算法的实现和总结
    算法纲要
    UVa401 回文词
    UVa 10361 Automatic Poetry
    UVa 537 Artificial Intelligence?
    UVa 409 Excuses, Excuses!
    UVa 10878 Decode the tape
  • 原文地址:https://www.cnblogs.com/shenlanzifa/p/5288778.html
Copyright © 2011-2022 走看看