LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2004, 03:53 AM   #1
pingswept
Member
 
Registered: May 2003
Location: Cambridge, MA
Distribution: SuSE, RedHat, and OpenBSD
Posts: 116

Rep: Reputation: 15
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
 
Old 05-23-2004, 08:27 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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;
}
 
Old 05-23-2004, 01:04 PM   #3
pingswept
Member
 
Registered: May 2003
Location: Cambridge, MA
Distribution: SuSE, RedHat, and OpenBSD
Posts: 116

Original Poster
Rep: Reputation: 15
That seems easy enough.

Thanks, Itsyou86!
 
Old 05-24-2004, 07:50 AM   #4
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
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?
 
Old 05-24-2004, 07:55 AM   #5
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
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);
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in recvfrom() live_dont_exist Programming 5 05-08-2005 04:25 PM
timing a function saajii Programming 1 10-25-2004 02:37 AM
recvfrom() takes 20 ms to return? pingswept Programming 3 05-25-2004 03:18 AM
libc function calls h/w Programming 4 02-13-2004 03:00 PM
using timer for repeated function calls poojamanu Programming 1 04-09-2003 08:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration