通过在起始时刻和停止时刻分别调用下面的代码,返回的时间相减,便可获得这段时间的毫秒数,其跨Windows、Linux、VxWorks三个平台。
#ifdef WIN32
#define OS_WINDOWS WIN32
#include <windows.h>
#endif
#ifdef LINUX
#include <unistd.h>
#include <sys/time.h>
#include <netinet/in.h>
#endif
#ifdef VXWORKS
#include "vxworks.h"
#include <tickLib.h>
#include <sysLib.h>
#endif
#define ULONAG unsigned long
ULONGA getTickCount(void)
{
ULONGA currentTime;
#ifdef WIN32
currentTime = GetTickCount();
#endif
#ifdef LINUX
struct timeval current;
gettimeofday(¤t, NULL);
currentTime = current.tv_sec * 1000 + current.tv_usec/1000;
#endif
#ifdef OS_VXWORKS
ULONGA timeSecond = tickGet() / sysClkRateGet();
ULONGA timeMilsec = tickGet() % sysClkRateGet() * 1000 / sysClkRateGet();
currentTime = timeSecond * 1000 + timeMilsec;
#endif
return currentTime;
}