⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_38684.htm

📁 vc
💻 HTM
字号:
<p>
序号:38684 发表者:trevor 发表日期:2003-05-05 13:35:01
<br>主题:新手提问:TC里面怎样获得系统时间?
<br>内容:很高兴成为大家中的一员,有个问题:在TC里怎么样才能获得系统时间呢?有没有固定的库函数,请高手指教,谢谢!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:微笑的撒旦 回复日期:2003-05-05 13:52:08
<br>内容:时间日期函数,函数库为time.h、dos.h<BR>在时间日期函数里,主要用到的结构有以下几个:<BR>总时间日期贮存结构tm:<BR>┌──────────────────────┐<BR>│struct tm?????????????????????????????????? │<BR>│{?????????????????????????????????????????? │<BR>│ int tm_sec;?? /*秒,0-59*/????????????????? │<BR>│ int tm_min;?? /*分,0-59*/????????????????? │<BR>│ int tm_hour;? /*时,0-23*/????????????????? │<BR>│ int tm_mday;? /*天数,1-31*/??????????????? │<BR>│ int tm_mon;?? /*月数,0-11*/??????????????? │<BR>│ int tm_year;? /*自1900的年数*/???????????? │<BR>│ int tm_wday;? /*自星期日的天数0-6*/??????? │<BR>│ int tm_yday;? /*自1月1日起的天数,0-365*/?? │<BR>│ int tm_isdst; /*是否采用夏时制,采用为正数*/│<BR>│}?????????????????????????????????????????? │<BR>└──────────────────────┘<BR>时间贮存结构time:<BR>┌────────────────┐<BR>│struct time???????????????????? │<BR>│{?????????????????????????????? │<BR>│ unsigned char ti_min;? /*分钟*/│<BR>│ unsigned char ti_hour; /*小时*/│<BR>│ unsigned char ti_hund;???????? │<BR>│ unsigned char ti_sec;? /*秒*/? │<BR>│??????????????????????????????? │<BR>└────────────────┘<BR>日期贮存结构date:<BR>┌───────────────┐<BR>│struct date?????????????????? │<BR>│{???????????????????????????? │<BR>│ int da_year; /*自1900的年数*/│<BR>│ char da_day; /*天数*/??????? │<BR>│ char da_mon; /*月数 1=Jan*/? │<BR>│}???????????????????????????? │<BR>└───────────────┘<BR>char????? *ctime(long *clock)<BR>?本函数把clock所指的时间(如由函数time返回的时间)转换成下列格式的字符串:Mon Nov 21 11:31:54 1983\n\0<BR>char????? *asctime(struct tm *tm)<BR>本函数把指定的tm结构类的时间转换成下列格式的字符串:Mon Nov 21 11:31:54 1983\n\0<BR>double???? difftime(time_t time2,time_t time1)<BR>计算结构time2和time1之间的时间差距(以秒为单位)<BR>struct tm *gmtime(long *clock)<BR>本函数把clock所指的时间(如由函数time返回的时间) 转换成格林威治时间,并以tm结构形式返回<BR>struct tm *localtime(long *clock)<BR>本函数把clock所指的时间(如函数time返回的时间) 转换成当地标准时间,并以tm结构形式返回<BR>void?????? tzset()<BR>本函数提供了对UNIX操作系统的兼容性<BR>long?????? dostounix(struct date *dateptr,struct time *timeptr)<BR>本函数将dateptr所指的日期,timeptr所指的时间转换成UNIX格式,并返回自格林威治时间1970年1月1日凌晨起到现在的秒数<BR>void?????? unixtodos(long utime,struct date *dateptr,struct time *timeptr)<BR>本函数将自格林威治时间1970年1月1日凌晨起到现在的秒数utime转换成DOS格式并保存于用户所指的结构dateptr和timeptr中<BR>void?????? getdate(struct date *dateblk)<BR>本函数将计算机内的日期写入结构dateblk中以供用户使用<BR>void?????? setdate(struct date *dateblk)<BR>本函数将计算机内的日期改成由结构dateblk所指定的日期<BR>void?????? gettime(struct time *timep)<BR>本函数将计算机内的时间写入结构timep中,以供用户使用<BR>void?????? settime(struct time *timep)<BR>本函数将计算机内的时间改为由结构timep所指的时间<BR>long?????? time(long *tloc)<BR>本函数给出自格林威治时间1970年1月1日凌晨至现在所经过的秒数,并将该值存于tloc所指的单元中.<BR>int??????? stime(long *tp)<BR>本函数将tp所指的时间(例如由time所返回的时间)写入计算机中.
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:微笑的撒旦 回复日期:2003-05-05 13:53:03
<br>内容:http://smallwhite.myrice.com/house/prgclass/C/Tclib3.htm
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:trevor 回复日期:2003-05-05 20:21:40
<br>内容:我把遇到的问题详细阐述一下:有两个算法来解决同一个问题,为了看看哪个效率更高,需要在程序开始和结束时获得系统时间,然后计算程序的运行时间长度。因为程序运行时间较短,所以最好获得的时间能够精确到毫秒或微妙,请问TC里面有没有这样的函数?我对已经得到的答复深表感谢,请针对这一具体情况作出进一步的指教,不胜感激!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:sdfsd 回复日期:2003-05-05 22:50:27
<br>内容:可以用一下clock_t(仍不能精确到毫秒),一秒为18.2个时钟周期:<BR>#include &lt;stdio.h&gt;<BR>#include &lt;stdlib.h&gt;<BR>#include &lt;time.h&gt;<BR><BR>void sleep( clock_t wait );<BR><BR>void main( void )<BR>{<BR>&nbsp;&nbsp; long&nbsp;&nbsp;&nbsp;&nbsp;i = 6000000L;<BR>&nbsp;&nbsp; clock_t start, finish;<BR>&nbsp;&nbsp; double&nbsp;&nbsp;duration;<BR>&nbsp;&nbsp; <BR>&nbsp;&nbsp; /* Measure the duration of an event. */<BR>&nbsp;&nbsp; printf( "Time to do %ld empty loops is ", i );<BR>&nbsp;&nbsp; start = clock();<BR>&nbsp;&nbsp; while( i-- ) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;<BR>&nbsp;&nbsp; finish = clock();<BR>&nbsp;&nbsp; duration = (double)(finish - start) / CLOCKS_PER_SEC;<BR>&nbsp;&nbsp; printf( "%f seconds\n", duration );<BR>}<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:Neil Gan 回复日期:2003-05-06 07:33:50
<br>内容:Because Windows is not a real time operation system, you cann't be sure when a thread will be scheduled. Normally, we compare algorithm by doing it multiple times. The following program do the two algorithms 1000000 times.<BR><BR>void Algorithm01(void);<BR>void Algorithm02(void);<BR><BR>void CompareAlgorithm(void)<BR>{<BR>long lStart=time(NULL);<BR>int i;<BR><BR>for (i=0 ; i &lt; 1000000 ; i++)<BR>&nbsp;&nbsp; Algorithm01();<BR>printf("Algorithm01: %d\n",time(NULL)-lStart);<BR><BR>lStart=time(NULL);<BR>for (i=0 ; i&lt;1000000 ; i++)<BR>&nbsp;&nbsp; Algorithm02();<BR>printf("Algorithm02: %d\n",time(NULL)-lStart);<BR>}<BR><BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -