DC 的 time1(), time2()
(高顯忠, sjgau4311@gmail.com, 2010-12-21 12:06)
1樓
#include <cstdlib>
#include <iostream>
#include <sys/timeb.h>
#include <time.h>
using namespace std;
// ----------------------------------------------
void time1(int *t1)
{
struct _timeb timebuffer;
int s1, s2, s3;
_ftime( &timebuffer );
s1= timebuffer.time;
s2= s1%(24L*86400);
s3= s2*1000 + timebuffer.millitm;
(*t1)= s3;
}// end of time1()
// ----------------------------------------------
void time2(int t1, double *dt)
{
// get new time of t2
int t2;
time1(&t2);
(*dt)= ((double) (t2 - t1))/1000.0;
// (*dt) >= 0.0
if ((*dt) < 0.0) {
(*dt)+= 24.0*86400.0;
}
}// end of time2()
// ----------------------------------------------
int main(int argc, char *argv[])
{
int no, t1, sum, i;
double dt;
no= 10;
while (no > 0) {
// get sum. of 1 + 2 + . . . + no
time1(&t1);
sum= 0;
for (i=1;i<=no;i++) {
sum+= i;
}
time2(t1, &dt);
printf("no= %12ld, sum= %12ld, dt= %10.3lf\n", no, sum, dt);
no= no + no;
}
printf("\n *** exit the loop, \n no= %12ld, sum= %12ld, dt= %10.3lf\n",
no, sum, dt);
system("pause");
return EXIT_SUCCESS;
}// end of main()