LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   timing function calls in C? (recvfrom() on Pentium II) (https://www.linuxquestions.org/questions/programming-9/timing-function-calls-in-c-recvfrom-on-pentium-ii-184732/)

pingswept 05-23-2004 03:53 AM

timing function calls in C? (recvfrom() on Pentium II)
 
Anyone know how I can measure how long it takes for a function to execute?

I'm writing a server application in C that calls recvfrom() to grab UDP packets. On the crappy old Pentium II I'm running it on, recvfrom() takes ~15 ms to execute. I can measure this by looking at the time in between serial port transmissions on either side of the call with an oscilloscope. However, I'd like a better method, so I can test it on multiple machines without dragging the scope around. I'm running OpenBSD 3.4. Suggestions?

By the way, does 15 ms seem slow for a recvfrom() call on a Pentium II, 233 MHz?

Thanks,
Pingswept

itsme86 05-23-2004 08:27 AM

Code:

int main(void)
{
  struct timeval t1, t2;

  gettimeofday(&t1, NULL);
  call_some_function();
  gettimeofday(&t2, NULL);

  printf("Start time: %d.%06d\n", t1.sec, t1.usec);
  printf("End time: %d.%06d\n",  t2.sec, t2.usec);

  return 0;
}


pingswept 05-23-2004 01:04 PM

That seems easy enough.

Thanks, Itsyou86!

cmfarley19 05-24-2004 07:50 AM

I have a similar need for code like this.
When I tried compiling the following:
Code:

#include <stdio.h>
#include <sys/time.h>


int main(void)
{
  struct timeval t1, t2;

  gettimeofday(&t1, NULL);
//  call_some_function();
  gettimeofday(&t2, NULL);

  printf("Start time: %d.%06d\n", t1.sec, t1.usec);
  printf("End time: %d.%06d\n",  t2.sec, t2.usec);

  return 0;
}

via:
Code:

$ gcc -o test main.c
main.c: In function `main':
main.c:13: structure has no member named `sec'
main.c:13: structure has no member named `usec'
main.c:14: structure has no member named `sec'
main.c:14: structure has no member named `usec'

Any thoughts?

cmfarley19 05-24-2004 07:55 AM

Found it...
Code:

printf("Start time: %d.%06d\n", t1.sec, t1.usec);
  printf("End time: %d.%06d\n",  t2.sec, t2.usec);

needs to be:
Code:

printf("Start time: %d.%06d\n", t1.tv_sec, t1.tv_usec);
  printf("End time: %d.%06d\n",  t2.tv_sec, t2.tv_usec);



All times are GMT -5. The time now is 05:29 PM.