LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-23-2010, 10:27 AM   #1
liqxpil
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Rep: Reputation: 0
output time in ms from gettimeofday


hi everyone

i am trying to print the time in ms using the gettimeofday function, but on execution the shell gives:

Segmentation fault (core dumped) error

and leaves the program.. any idea how to fix this??
here is the code:


struct timeval time1,time2;
double time_used1;

//get time1
gettimeofday(&time1, NULL);

rc = connect(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));

//get time2
gettimeofday(&time2, NULL);

time_used1 = time_diff(timer1, timer2);

printf("Connection established within: %s ms\n",time_used1);//print time



//calculate difference
double time_diff(struct timeval prior, struct timeval latter)
{
return
(latter.tv_usec - prior.tv_usec) / 1000. +
(latter.tv_sec - prior.tv_sec) * 1000.;
}
 
Old 04-23-2010, 10:34 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Try this:
Code:
printf("Connection established within: %lf ms\n",time_used1);//print time
 
Old 04-23-2010, 10:47 AM   #3
liqxpil
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Original Poster
Rep: Reputation: 0
it works now but it outputs the time not the difference, something like

1708494572214882408780026684122871205398765820921799245824.000000 ms

i will check the difference function..

thanks alot
 
Old 04-23-2010, 02:34 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, again:

Additional suggestions:

1. Use a prototype for "time_diff()" (or simply move it to the top of your program

2. Use explicit casts (instead of allowing implicit conversion).

3. Pass by reference (instead of by value)


For example:
Code:
struct timeval time1,time2;
double time_used1;

double time_diff(struct timeval * prior, struct timeval * latter);

...
  //get time1
  gettimeofday(&time1, NULL);

  rc = connect(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));

  //get time2
  gettimeofday(&time2, NULL);

  time_used1 = time_diff(&timer1, &timer2);

  printf("Connection established within: %s ms\n",time_used1);//print time


//calculate difference
double time_diff(struct timeval * prior, struct timeval * latter)
{
  double x =
   (double)(latter->tv_usec - prior->tv_usec) / 1000.0L +
   (double)(latter->tv_sec - prior->tv_sec) * 1000.0L;
  return x;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
BASH Millisecond timing - gettimeofday JavaNinja Linux - Newbie 6 05-13-2012 07:30 PM
gettimeofday versus TSC register - coding RedOctober45 Linux - Software 2 08-27-2009 01:57 PM
gettimeofday() - execution time and underlying implementation details cagri_balkesen Linux - Kernel 1 02-08-2009 02:27 PM
a bug of gettimeofday? George2 Programming 13 02-13-2007 09:34 AM
time, gettimeofday h/w Programming 13 12-08-2003 06:17 PM

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

All times are GMT -5. The time now is 12:26 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration