Thread: Time functions

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    4

    Time functions

    Is there any timing functions that will give me milliseconds.

    gettimeofday() does microseconds,
    and time() gives seconds at best precision

    Anybody know of any that gives milliseconds?
    Thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    clock()
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    clock has an issue. How fine-grained the result can be is left up to the compiler. In other words, you may get ms in 1000 or increments. You need to check tour results.

    You are better off doing something like this with gettimeofday() to get to milliseconds:
    Code:
    #include <time.h>
    int pointless(void)
    {
        unsigned int i=0;
        while(i< 0xffffffe)
        {
        	 ++i;
        }
        printf("%d\n",i);
        return i;
    
    }
    int main()
    {
              struct timeval  first,
                               second,
                               lapsed;
    
               struct timezone tzp;
    
                  gettimeofday (&first, &tzp);
    
               /* lapsed time ...... */
               pointless();
    
               gettimeofday (&second, &tzp);
    
               if (first.tv_usec > second.tv_usec)
               {
                  second.tv_usec += 1000000;
                  second.tv_sec--;
               }
               lapsed.tv_usec = second.tv_usec - first.tv_usec;
               lapsed.tv_sec  = second.tv_sec  - first.tv_sec;
               printf("Millseconds=%d\n",lapsed.tv_sec*1000 + lapsed.tv_usec /1000);
               return 0;
    }

  4. #4
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    You probably could just make up a random number for milliseconds, it'll be close enough to real. lol

    ---EDIT---
    Code:
    #include <sys/time.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int pointless(void) {
        unsigned int i=0;
        while(i < 0xffffffe) {
             ++i;
        }
        printf("%d\n", i);
        return i;
    }
    
    int main() {
     struct timeval first, second, lapsed;
    
    	struct timezone tzp;
     gettimeofday (&first, &tzp);
    
     /* lapsed time ...... */
     pointless();
    
     gettimeofday (&second, &tzp);
    
     if (first.tv_usec > second.tv_usec)
     {
      second.tv_usec += 1000000;
      second.tv_sec--;
     }
     lapsed.tv_usec = second.tv_usec - first.tv_usec;
     lapsed.tv_sec  = second.tv_sec  - first.tv_sec;
     printf("Jim's Milliseconds = %d\n", lapsed.tv_sec*1000 + lapsed.tv_usec /1000);
    
     srand(time(NULL));
     printf("Kleid's Milliseconds = %d\n", rand() % 1500);
     return 0;
    }
    And as you may see, my approach is not error free:
    Code:
    kleid@Shiva:~/Programming/Laboratory$ ./a.out
    268435454
    Jim's Milliseconds = 513
    Kleid's Milliseconds = 384
    kleid@Shiva:~/Programming/Laboratory$ ./a.out
    268435454
    Jim's Milliseconds = 511
    Kleid's Milliseconds = 1456
    kleid@Shiva:~/Programming/Laboratory$ ./a.out
    268435454
    Jim's Milliseconds = 512
    Kleid's Milliseconds = 1393
    kleid@Shiva:~/Programming/Laboratory$ ./a.out
    268435454
    Jim's Milliseconds = 509
    Kleid's Milliseconds = 1100
    kleid@Shiva:~/Programming/Laboratory$
    Last edited by Kleid-0; 01-17-2005 at 02:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  2. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  3. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  4. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  5. help with functions (first time)
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 03-06-2002, 09:37 PM