GetTime()获取的时间单位是什么?秒,毫秒,纳秒?
是从1970年到现在的秒数吗?
time = GetTime();
for(c=0;c<10000;c++){
Print("hello world") ;
}
time2 = GetTime();
Print((double)time-(double)time2) ;
GetTime()获取的时间单位是什么?
Re: GetTime()获取的时间单位是什么?
来源于网络:使用常规旧 timeGetTime() 做计时不可靠许多基于 Windows 的操作系统上,因为系统计时器的粒度可高达 10-15 毫秒,也就是说, timeGetTime() 只是精确到 10-15 毫秒为单位)。[注高粒度像 Windows NT 中,基于 NT 的运作系统出现于 2000 年和 XP。Windows 95 和 98 往往有很多好的粒度,约 1 5 毫秒。]
而我们查看Time的源码:http://myecere.googlecode.com/svn/trunk ... ys/Time.ec,是调用了windows的api,这样的话,时间太不准确了
public Time GetTime(void)
{
#if defined(__WIN32__)
return timeGetTime() / 1000.0;
#elif defined(__unix__) || defined(__APPLE__)
struct timeval tp;
struct timezone tzp;
static int secbase = 0;
gettimeofday(&tp, &tzp);
if(!secbase)
{
secbase = tp.tv_sec;
return tp.tv_usec / 1000000.0;
}
return (tp.tv_sec - secbase) + tp.tv_usec / 1000000.0;
#endif
}
而我们查看Time的源码:http://myecere.googlecode.com/svn/trunk ... ys/Time.ec,是调用了windows的api,这样的话,时间太不准确了
public Time GetTime(void)
{
#if defined(__WIN32__)
return timeGetTime() / 1000.0;
#elif defined(__unix__) || defined(__APPLE__)
struct timeval tp;
struct timezone tzp;
static int secbase = 0;
gettimeofday(&tp, &tzp);
if(!secbase)
{
secbase = tp.tv_sec;
return tp.tv_usec / 1000000.0;
}
return (tp.tv_sec - secbase) + tp.tv_usec / 1000000.0;
#endif
}
Re: GetTime()获取的时间单位是什么?
Hi janksenhu,
GetTime() returns the time in seconds, from no particular moment (In Windows it happens to be the time since Windows was started, wrapping around every 49.71 days). It is meant to be used as delta only, from an earlier moment.
For getting the current time, use:
I just noticed, this supports only second precision (no milliseconds).
To convert this into seconds since 1970, use:
For cases requiring high precision, try setting the GuiApplication's timerResolution property.
If you have:
This ends up calling timeSetEvent() on Windows, which I think should increase the precision.
At some point in the past we were using timeBeginPeriod() to change that resolution, but that code is commented out right now.
Please let me know if you run into a limitation or if you find a way to improve on this
Regards,
Jerome
GetTime() returns the time in seconds, from no particular moment (In Windows it happens to be the time since Windows was started, wrapping around every 49.71 days). It is meant to be used as delta only, from an earlier moment.
For getting the current time, use:
Code: Select all
DateTime now;
now.GetLocalTime();
To convert this into seconds since 1970, use:
Code: Select all
SecSince1970 t;
t = now;
If you have:
Code: Select all
define app = (GuiApplication)__thisModule;
app.timerResolution = 60; // 60 hertz
At some point in the past we were using timeBeginPeriod() to change that resolution, but that code is commented out right now.
Please let me know if you run into a limitation or if you find a way to improve on this
Regards,
Jerome