怎样获取毫秒级时间(C/C++)

dnfsky7 2013-09-06 12:29:18
目的:获取毫秒级别时间,要求精确度高,稳定性好 VC++2010
...全文
1677 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
modyaj 2013-09-06
  • 打赏
  • 举报
回复
API吧 VOID GetLocalTime(LPSYSTEMTIME lpSystemTime ) 百度一下具体细节
max_min_ 2013-09-06
  • 打赏
  • 举报
回复


usleep();

select();
//可精确至百万分之一秒
赵4老师 2013-09-06
  • 打赏
  • 举报
回复
Section: POSIX Programmer's Manual (P) Updated: 2003 -------------------------------------------------------------------------------- NAME sys/timeb.h - additional definitions for date and time SYNOPSIS #include <sys/timeb.h> DESCRIPTION The <sys/timeb.h> header shall define the timeb structure that includes at least the following members: time_t time The seconds portion of the current time. unsigned short millitm The milliseconds portion of the current time. short timezone The local timezone in minutes west of Greenwich. short dstflag TRUE if Daylight Savings Time is in effect. The time_t type shall be defined as described in <sys/types.h> . The following shall be declared as a function and may also be defined as a macro. A function prototype shall be provided. int ftime(struct timeb *); (LEGACY ) The following sections are informative. APPLICATION USAGE None. RATIONALE None. FUTURE DIRECTIONS None. SEE ALSO <sys/types.h> , <time.h> COPYRIGHT Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . --------------------------------------------------------------------------------
赵4老师 2013-09-06
  • 打赏
  • 举报
回复
_ftime Gets the current time. void _ftime( struct _timeb *timeptr ); Function Required Header Compatibility _ftime <sys/types.h> and <sys/timeb.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value _ftime does not return a value, but fills in the fields of the structure pointed to by timeptr. Parameter timeptr Pointer to _timeb structure Remarks The _ftime function gets the current local time and stores it in the structure pointed to by timeptr. The _timeb structure is defined in SYS\TIMEB.H. It contains four fields: dstflag Nonzero if daylight savings time is currently in effect for the local time zone. (See _tzset for an explanation of how daylight savings time is determined.) millitm Fraction of a second in milliseconds. time Time in seconds since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC). timezone Difference in minutes, moving westward, between UTC and local time. The value of timezone is set from the value of the global variable _timezone (see _tzset). Example /* FTIME.C: This program uses _ftime to obtain the current * time and then stores this time in timebuffer. */ #include <stdio.h> #include <sys/timeb.h> #include <time.h> void main( void ) { struct _timeb timebuffer; char *timeline; _ftime( &timebuffer ); timeline = ctime( & ( timebuffer.time ) ); printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] ); } Output The time is Tue Mar 21 15:26:41.341 1995 Time Management Routines See Also asctime, ctime, gmtime, localtime, time
dnfsky7 2013-09-06
  • 打赏
  • 举报
回复
9楼 有什么详细的介绍的链接吗
木头菇 2013-09-06
  • 打赏
  • 举报
回复
引用 4 楼 sduxiaoxiang 的回复:
GetTickCount就可以到ms了
这是15ms级别的,不是1ms级别的
lyramilk 2013-09-06
  • 打赏
  • 举报
回复
x86可以用rdtsc指令取,取完除一下CPU的主频就是整秒,拿它来算,没有比这个更敏感的了。连着取两次也保证能计算出时间差来。不过这个只适合取时间差。
dnfsky7 2013-09-06
  • 打赏
  • 举报
回复
QueryPerformanceCounter 纳秒级 这中并不稳定吧
赵4老师 2013-09-06
  • 打赏
  • 举报
回复 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt||0==pszFmt[0]) return;
    _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
        } else {
            fclose(flog);
        }
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
#endif
    for (i=0;i<10000;i++) {
        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
    }
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中
Kaile 2013-09-06
  • 打赏
  • 举报
回复
QueryPerformanceCounter 纳秒级
sduxiaoxiang 2013-09-06
  • 打赏
  • 举报
回复
GetTickCount就可以到ms了
dnfsky7 2013-09-06
  • 打赏
  • 举报
回复
我先用VC试试

64,681

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧