Measuring time

This is a discussion on Measuring time within the C++ Programming forums, part of the General Programming Boards category; hi could anyone let me know an efficient way to measure time? e.g. i want my program to stay in ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    9

    Measuring time

    hi could anyone let me know an efficient way to measure time?

    e.g. i want my program to stay in a loop for exactly 20 seconds, what would be the best way to do that?

    thanks.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Before starting the loop, get and save the current system time. This is your start time. Then, everytime through the loop, get the current system time and compare it to the start time. I won't tell you what methods to use because every system and OS have different ways of getting and representing the system time.

    PK

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,673
    If you need more help, state your OS and compiler and if you really need "exactly" 20 seconds or just really close to it.

    gg

  4. #4
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Code:
    #include<time.h>
    ...
    time_t end = time(0)+20;
    while(time(0)<end){
    //your loop
    }

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,673
    >>+20
    There are no arithmetic properties defined for type time_t. You should use difftime().

    gg

  6. #6
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Codeplug
    >>+20
    There are no arithmetic properties defined for type time_t. You should use difftime().

    gg
    ah?!?!
    ...
    from time.h
    Code:
    /* Define the implementation defined time type */
    
    #ifndef _TIME_T_DEFINED
    typedef long time_t;        /* time value */
    #define _TIME_T_DEFINED     /* avoid multiple def's of time_t */
    #endif
    What did you say??

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Busted!

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Once again, I quote Prelude: "My compiler does this" != "The standard says this". I can't tell you what the standard says about the subject, but quoting your compiler's source code is no proof that you are correct... For all you know, on another compiler/system, time_t may be a struct or union, or even a pointer. While in the former cases you'd have a compiler error to catch you, in the latter case you would end up with a nasty bug that would be incredibly difficult to find, especially if you assume that time_t is just a long and therefore eliminate it as a potential problem.

    **EDIT**
    >>/* Define the implementation defined time type */
    This strongly supports my argument. It means that any compiler-writer is free to implement the time_t however they wish, as long as it functions in the same way that the standard defines.
    Last edited by Hunter2; 10-06-2004 at 10:46 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Actually, the standard states that time_t is an arithmetic type.

    typedef a-type time_t;

    The type is the arithmetic type a-type of an object that you declare to hold the value returned by time. The value represents calendar time.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,673
    Adding to a string date

    Draw your own conclusions.

    gg

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    another standard holy-war, but I think Prelude answered it best (as she usually does):

    Quote Originally Posted by Prelude
    time_t must be an arithmetic type because sometimes values of type time_t have to be compared with -1. However, the standard says nothing about the arithmetic properties of time_t aside from that, so while t1 < t2 will probably work, the result isn't guaranteed to be portable...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    time
    Syntax
    #include <time.h>
    time_t time(time_t *t);

    Description
    If t is not NULL, the current time is stored in *t.

    Return Value
    The current time is returned.

    Portability
    ANSI, POSIX

    Example
    printf("Time is %d\n", time(0));

    I sincerely doubt that anyone you be smart enough to write a time funtion, with the standard prototipe that doesn't manipulate time_t as long. NOTE that time(...) return the time. structs can't be returned em C, although this is C++, and I'm not aware of any time.h for C++ or <time> that might have a struct declaration for time_t.

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,673
    If you don't care about portability or adhering to the standard, then do whatever works.

    And you can return structs in C and C++.

    gg

  14. #14
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    http://www.cs.tut.fi/~leopold/Boar/AnsiLib/time.html

    <time.h> provides the user tools for manipulating dates and time.

    (...)
    clock_t: An arithmetic type representing times.
    time_t: An arithmetic type representing times.
    struct tm: A datatype capable of holding the components of a calendar time. It has at least the following members that the user of the library may access:
    int tm_sec: Seconds after the minute (0...59)
    int tm_min: Minutes after the hour (0..59)
    int tm_hour: Hours since midnight (0..23)
    int tm_mday: Day of the month (1..31)
    int tm_mon: Months since January (0..11)
    int tm_year: Years since 1900
    int tm_wday: Days since Sunday (0..6)
    int tm_yday: Days since January 1st (0..365)
    int tm_isdst: Daylight Saving Time flag. This flag will be positive if Daylight Savings Time is in effect, and zero if not. If the information is not available, tm_isdst is negative.
    (...)


    Who said structs can't be returned in C++??

    On DJGPP <sys\djtypes.h>
    typedef unsigned int time_t;

    All I find are arithmetic definitions of time_t.
    Please post a link with a time_t definition diferent from the integer one.
    Last edited by xErath; 10-07-2004 at 07:33 AM.

  15. #15
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    either way, running a loop and checking with ever iteration eats your processor time like you wouldn't believe, and you can't do anything outside the loop while it's running (unless you spawn another thread), but since it seems like you want to work inside the loop anyway, code on!
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21