GetTime()获取的时间单位是什么?

来自中国的朋友,欢迎您在本版面使用中文讨论问题。请注意,如果您想得到不懂中文的人的帮助,请同时提供英文译文。
Help and discussions in Chinese.
Post Reply
janksenhu
Posts: 6
Joined: Sat Feb 02, 2013 2:45 am

GetTime()获取的时间单位是什么?

Post by janksenhu »

GetTime()获取的时间单位是什么?秒,毫秒,纳秒?
是从1970年到现在的秒数吗? :D

time = GetTime();
for(c=0;c<10000;c++){
Print("hello world") ;
}
time2 = GetTime();
Print((double)time-(double)time2) ;
janksenhu
Posts: 6
Joined: Sat Feb 02, 2013 2:45 am

Re: GetTime()获取的时间单位是什么?

Post by janksenhu »

来源于网络:使用常规旧 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
}
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: GetTime()获取的时间单位是什么?

Post by jerome »

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:

Code: Select all

DateTime now;
now.GetLocalTime();
I just noticed, this supports only second precision (no milliseconds).

To convert this into seconds since 1970, use:

Code: Select all

SecSince1970 t;
t = now;
For cases requiring high precision, try setting the GuiApplication's timerResolution property.

If you have:

Code: Select all

define app = (GuiApplication)__thisModule;
app.timerResolution = 60; // 60 hertz
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
Post Reply