GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Apr-2006, 22:45
smoothdogg00 smoothdogg00 is offline
Junior Member
 
Join Date: Mar 2006
Posts: 31
smoothdogg00 is on a distinguished road
Exclamation

Measuring time? Help!


I just need to write a simple program to measure time, but am not sure what I'm doing wrong.

CPP / C++ / C Code:
main(int argc, char * argv[])
{
  struct timeval * tv;
  struct timezone * tz;

  gettimeofday(tv, tz);
  usleep(1000);
  gettimeofday(tv, tz);

  printf("%d", *tv->suseconds_t);
  printf("%d", *tv->suseconds_t);
}

The error is (probably obviously) where I am trying to access suseconds_t from the timeval structure.

Thanks for your help.
  #2  
Old 10-Apr-2006, 09:19
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough

Re: Measuring time? Help!


Please post any errors messages verbatim, as this will make it possible for others to help more readily even if they have no experience with gettimeofday() or timeval structure.

Moving on, I see the following problems (note that some may not really be problems in your program if there are lines of code that you did not post, I am just assuming you posted it all to be safe):
  1. missing include directives
  2. missing return type for main() function
  3. uninitialized pointers (gettimeofday() does not create the structures, just modifies them)
  4. timestamp from before sleep is overwritten by timestamp recorded after sleep
  5. cannot read output because there is not space between tokens
  6. timeval has no field named suseconds_t, but it does have tv_usec
  7. cannot derefence a non-pointer type (timeval->tv_usec is not a pointer)
  8. missing return value for main() function
CPP / C++ / C Code:
#include <cstdio>  // <1> for printf()
#include <unistd.h>  // <1> for usleep()
#include <sys/time.h>  // <1> for timeval, timezone, and gettimeofday()

int main (int argc, char* argv[])  // <2>
{
    timezone* tz = new timezone;  // <3>
    timeval* tv_before = new timeval;  // <3,4> for timestamp before sleep
    timeval* tv_after = new timeval;  // <3,4> for timestamp after sleep

    gettimeofday (tv_before, tz);  // <4>
    usleep (1000);
    gettimeofday (tv_after, tz);  // <4>

    printf ("%d\n", tv_before->tv_usec);  // <4,5,6,7>
    printf ("%d\n", tv_after->tv_usec);  // <4,5,6,7>

    return 0;  // <8>
}
Let me know if you have questions.

Matthew

P.S. I used <cstdio> and operator new and removed the (unecessary) keyword struct because this is the C++ forum. A few small changes, such as reintroducing struct and using malloc/free, could make this a correct C program.
__________________
I was born not knowing and have only had a little time to change that here and there. -- Richard P. Feynman

Boris Podolsky: James! How's the rat business?
James Moreland: Well, actually it's mostly students I'm experimenting on now.
Kurt Gödel: My God, the mazes must be enormous.
Last edited by QED : 10-Apr-2006 at 10:24. Reason: To clarify the code sample.
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a time converter tylerfelix C++ Forum 15 20-Nov-2005 21:34
Simulation Problem wu_weidong C++ Forum 7 12-Mar-2005 23:56
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
time Problem zuzupus MySQL / PHP Forum 9 24-Jul-2003 08:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:19.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.