※ 引述《brad (Zzzzzzzz)》之銘言:
: 在 VC++ 中, 我目前查到僅可用 _ftime 取到 millisecond 的 time value,
: #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] );
: }
: 不曉得是否有其它的做法或 function 可以讓我取到 microsecond???
: 我有前人在 UNIX 上 run 的 code 可以取到 microsecond, 可惜目前
: window 平台的 VC++ 並不支援. 是否有人可以知道有關的做法跟這 code
: 的做法類似(並可以在 VC++ 上 run)........:)
: #include <sys/time.h>
: void time_check(type, cpu_time)
: int type; /* START or GET_TIME */
: float *cpu_time;
: {
: static int timei,timef;
: static int timedi, timedf, timesec;
: struct itimerval value1, ovalue1;
: if (type == 0) { /* Time Check. */
: value1.it_interval.tv_sec=0;
: value1.it_interval.tv_usec=0;
: value1.it_value.tv_sec=1000000L; /* second */
: value1.it_value.tv_usec=0; /* micro-second */
: setitimer(1,&value1,&ovalue1); /* 1: Virtual time */
: getitimer(1,&value1);
: timedi = value1.it_value.tv_sec;
: timei = value1.it_value.tv_usec;
: } else { /* GET_TIME */
: /* time check */
: getitimer(1,&value1);
: timedf = value1.it_value.tv_sec;
: timef = value1.it_value.tv_usec;
: timedf = timedi - timedf ; /* decrement */
: if (timedf == 0) {
: timesec = 0;
: timef = 0;
: *cpu_time = 0.0;
: }
: else {
: timef = timef -timei;
: timesec = timedf - 1;
: timef = 1000000 - timef;
: *cpu_time = timesec + timef/1000000.0;
: }
: }
: }
: Thanks for your help.........^_^........
這是以前寫的希望對您能派上用場! ^_^
#include <time.h>
#define tno 6
#define Hz 1
#define times_error -1
#define timer_error -2
#define ftimes_error (float)times_error
#define ftimer_error (float)timer_error
#define time_t long
struct tbuffer {
time_t proc_user_time ;
time_t proc_system_time ;
time_t child_user_time ;
time_t child_system_time ;
} _old_time[tno] = {{ 0,0,0,0},
{ 0,0,0,0},
{ 0,0,0,0},
{ 0,0,0,0},
{ 0,0,0,0},
{ 0,0,0,0}} ;
time_t print_time_continue(i)
int i ;
{
struct tbuffer time_rec ;
time_t x ;
if(i < 0 || i > (tno - 1)) {
printf(" Timer %d Not exist\n",i) ;
return(timer_error) ;
}
if(time(&time_rec.proc_user_time) == times_error) return(times_error) ;
x = time_rec.proc_user_time - _old_time[i].proc_user_time ;
printf("Using CPU Time ; %10.3f seconds\n",((float)x)/Hz) ;
return(x) ;
}
time_t reset_timer(i)
int i ;
{
struct tbuffer time_rec ;
if(i < 0 || i > (tno - 1)) {
printf(" Timer %d Not exist\n",i) ;
return(timer_error) ;
}
if(time(&time_rec.proc_user_time) == times_error) return(times_error) ;
_old_time[i].proc_user_time = time_rec.proc_user_time ;
return(time_rec.proc_user_time) ;
}
您可以試試看!
--
埏埴以為器,當其無,有器之用。
鑿戶牖以為室,當其無,有室之用。
兵強則滅,木強則折。堅強處下,柔弱處上。
天下莫柔於水,而攻堅強者莫之能勝,其無以易之。
弱之勝,柔之勝剛,天下莫不知,莫能行。
摘自老子<道德經>
--
※ Origin: 程式設計樂園 ◆ From: g2227a.dorm.ccu.edu.tw