1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include"timing.h"
//#include<sys/wait.h>
#include<stdio.h>
#include"pac.h"
#ifdef UNIX
static struct timeval *tp1,*tp2; //current and previous time
static struct tms *tmsp; //time struct
static double tot_tid; //time diffenence
static clock_t cl; //clock time
static int toTid; //time
static struct timespec remain;
#elif defined MSWIN
static DWORD *tp1,*tp2; //current and previous time
static double tot_tid; //time diffenence
#endif
#ifdef UNIX
#include<unistd.h>
#include<time.h>
//write a such struct
void printtp(struct timeval tp) {
#ifdef XWIN
printf("%d s %d u\n",tp.tv_sec,tp.tv_usec);
#endif
}
//write a tms
void printtms(struct tms t) {
#ifdef XWIN
double k=.016666666;
printf("%f usr, %f sys\n",k*t.tms_utime,k*t.tms_stime);
printf("%f usr, %f sys : children\n",k*t.tms_cutime,k*t.tms_cstime);
#endif
}
#endif
#ifdef UNIX
//difference between two times
double difftime(struct timeval tp1,struct timeval tp2) {
long a,b;
a=tp1.tv_sec-tp2.tv_sec;
b=tp1.tv_usec-tp2.tv_usec;
if (b<0) { a--; b = 1000000; }
return (b*0.000001) a;
}
#elif defined MSWIN
double difftime(DWORD tp1,DWORD tp2) { //difference between two times
return (tp1-tp2)*0.001;
}
#endif
void timingstart() { //initialize timing
#ifdef UNIX
tp1=new timeval; //allocate structures
tp2=new timeval;
tmsp=new tms;
//initialize
(*tp1).tv_sec=0;
(*tp1).tv_usec=0;
(*tp2).tv_sec=0;
(*tp2).tv_usec=0;
gettimeofday(tp1,NULL); //get the time
//printtp(*tp1);
//printf("start\n");
#elif defined MSWIN
tp1=new DWORD; //allocate structures
tp2=new DWORD;
*tp1=GetCurrentTime(); //get the time
#endif
}
// ----------------
void timing(int super){ //do timing/synchronization
super ; //divisor
if (!super) pacexit("super zero"); //if would then be division be zero
do {
#ifdef UNIX
gettimeofday(tp2,NULL); //get the time now
#elif defined MSWIN
*tp2=GetCurrentTime(); //get the time now
#endif
//printtp(*tp2);
tot_tid=difftime(*tp2,*tp1); //compute difference between moments
if (tot_tid<(0.25/super)) //if time interval not yet used up
#ifdef UNIX
remain.tv_sec = 0; remain.tv_nsec = ((0.25/super) - tot_tid) * 1000000000;
nanosleep(&remain, NULL); //sleep
#elif MSWIN
MSG msg;
if (PeekMessage(&msg, NULL, NULL, NULL,PM_NOREMOVE)) //if there is msg
if (GetMessage(&msg, NULL, NULL, NULL)) { //if message got
TranslateMessage(&msg); //else tranlate and
DispatchMessage(&msg); //dispatch the message
}
#else
; //nothing
#endif
//if mswin::peek/getmessage
} while (tot_tid<(0.25/super)); //while time interval not used up
//printf("\n\n\n");
//printf("The end. : %f\n",tot_tid);
#ifdef UNIX
cl=times(tmsp); //get the time
#endif
//printf("%d \n",cl);
//printtms(*tmsp);
//printf("-----\n");
*tp1=*tp2; //current time will be previous time
}
|