简体   繁体   中英

time in milliseconds in c99?

I'm trying to figure out how to calculate the milliseconds between some socket programming connections in C99 but getting 0 ms on the C99 and returns an actual value on the mac terminal. I included time parts of my code.

Init:

  struct timeval start,end;
  double t1,t2;
  t1 = 0.0;
  t2 = 0.0;

start of operation:

t1+=start.tv_sec+(start.tv_usec/1000000.0);

End of operation:

t2+=end.tv_sec+(end.tv_usec/1000000.0);

And then just printing the actual time in ms:

printf("Sent........RTT = %g ms\n",(t2-t1)/100);

Assuming you update the timeval structures correctly, you are not calculating milliseconds, but rather whole seconds and you print the difference divided by 100 instead of multiplied by 1000. You would loose less precision by computing the time differences:

diff += (end.tv_sec - start.tv_sec) * 1000.0 + end.tv_usec / 1000.0 - start.tv_usec / 1000.0;

...

printf("Sent........RTT = %g ms\n", diff / 100);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM