Thread: time.h & Delay

  1. #1
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265

    time.h & Delay

    I am traying to create a delay function using time.h

    Code:
    #include <time.h>
    
    void Delay (unsigned int Sec)
    {
           clock_t ticks1 = clock(), ticks2 = ticks1;
           while ( ( ticks2/CLOCKS_PER_SEC - ticks1/CLOCKS_PER_SEC ) < Sec)
               ticks2 = clock();
    }
    But as you see this function get seconds (int) and I want it to work with miliseconds (like delay & sleep).
    So how can I do that? (maybe another type (clock_t)?)

    Thanks.
    gavra.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you multiply ticks/CLOCKS_PER_SEC by 1000 you should get ticks/"CLOCKS_PER_MILLISEC", since there are 1000 milliseconds in a second. You may also need to do some casting to double, so as not to have the intermediate results (seconds) truncated.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To avoid having to work with delay and CLOCKS_PER_SEC, perhaps you could use
    difftime (which gives you seconds but it gives it to you as a double, not an int).

    (and btw, sleep() returns seconds, not milliseconds).

    Here's an example of difftime():

    Code:
    #include <time.h>
    #include <stdio.h>
    #include <dos.h>
    
    int main(void)
    {
       int c;
       time_t first, second;
    
       first = time(NULL);  /* Gets system  time */
       delay(2000);          /* Waits 2 secs */
       second = time(NULL); /* Gets system time again */
    
       printf("The difference is: %f seconds\n",difftime(second,first));
       c = getchar();
       return 0;
    }
    You'd want to only work with the part of the seconds after the decimal point, and it certainly wouldn't be any more accurate than delay. It would free you from having to work with CLOCKS_PER_SEC, (which would perhaps vary from one computer to the next), however.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Adak View Post
    (and btw, sleep() returns seconds, not milliseconds).
    On Windows, Sleep() takes the number of milliseconds to sleep.
    On UNIX, sleep() takes seconds.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by cpjust View Post
    On Windows, Sleep() takes the number of milliseconds to sleep.
    On UNIX, sleep() takes seconds.
    Sleep() was not mentioned, however:

    ... and I want it to work with miliseconds (like delay & sleep).
    The sleep() function still uses seconds on Windows.

  6. #6
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    No it's ms (on windows).
    Anyway thank you Adak (:

    Edit: humm it doesn't work O: infinite loop probably \:
    Code:
    void Delay (int ms)
    {
         time_t first = time(NULL), second = first;
         while (second - first < ms)
               second = time(NULL);
    }
    Where I go wrong?
    ---
    Anon, Adak is right ("which would perhaps vary from one computer to the next") but thank you too (8
    Last edited by gavra; 07-29-2008 at 03:20 AM.
    gavra.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #include <time.h>
    
    void Delay (unsigned int Sec)
    {
           clock_t ticks1 = clock(), ticks2 = ticks1;
           clock_t target = Sec * CLOCKS_PER_SEC;
           while ( ( ticks2 - ticks1) < target)
               ticks2 = clock();
    }
    See how we got rid of a whole heap of division? ;-) [Of course, it doesn't make much difference here, as we should really not be spending the entire time calling clock - that is pretty bad, really.]

    Note that whilst CLOCKS_PER_SEC is not guaranteed to be 1000 - it could be 50, 100, 643, 1142 or any other number the system implementer fancies using. It is COMMONLY 1000, but that's not the same as a guarantee by the standard.

    You could of course change your interface to take a millisecond value, and then adjust it by multiplying by CLOCKS_PER_SEC and dividing by 1000 - as long as you don't need excessively long periods (as in MANY DAYS, 46 days with CLOCKS_PER_SEC = 1000) it should be OK.

    In linux/unix, there is "usleep()" which gives you a sleep time in microseconds. It may not be as precise as the input value indicates, but it's definitely a better way to not waste all the CPU capacity on checking what time it is. Let some other task run if you don't want to do something.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Humm ok but you probably haven't seen my last post.
    I am traying to create a delay function with the same arguments as the Sleep function - why am I using time.h? cause I want it to be permanent (I mean that I want it to be the same between different computers).
    Clear? (:
    gavra.

  9. #9
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    What?!
    gavra.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think that post is spam, and I have reported it...

    I also think that my reply gives a sane suggestion as to how you could implement your code. However, I seriously suggest that you make a wrapper that calls the releveant sleep-type function on the platform you choose to use, rather than spinning on calls to clock() - it is wasting CPU time that can be used to do other useful work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    ohh
    I tried not using "clock" (as Adak has suggested) but it's no working and I really don't understand what "wrapper" means(?)
    (I am just traying to create a delay function @_@)
    gavra.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A "wrapper" function is a function that "hides the name" of a non-portable function, e.g. we have the functions linuxfunc (which is also available on other Unix) and WindowsFunc, which takes different arguments, and return different return values, but otherwise does the same thing, so we could do something like this:
    Code:
    int wrappedfunc(int a)
    {
    #if defined(WINDOWS)
       BOOL res;
       res = WindwosFunc(a, NULL);
       if (res) return 0;   // Success. 
       else return -1;     // Fail
    #elseif defined(LINUX) || defined(UNIX)
       return linuxfunc(a * 10);   // Returns -1 for fail, 0 for success. 
    #else 
       // We don't know what to do if it's not LINUX/UNIX, so let the compiler tell us
      #error "This only works for only Linux, Unix and Windows - please add code here to support other OS's" 
    #endif
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Do you mean something like this:
    Code:
    #ifdef linux
    #define clr() system("clear");
    #elif _WIN32
    #define clr() system("cls");
    #endif
    ?

    Anyway I don't care about the OS..
    "Sleep" influence changes between diffrent computers right?
    gavra.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    Do you mean something like this:
    Code:
    #ifdef linux
    #define clr() system("clear");
    #elif _WIN32
    #define clr() system("cls");
    #endif
    ?
    Yes, that's another way to do the same thing. Although I would try to avoid using system() to achieve something like that.

    Anyway I don't care about the OS..
    "Sleep" influence changes between diffrent computers right?
    If you mean that "Sleep" in Windows is different (besides capitalization) from the "sleep" in Linux, yes they are. And there is a "usleep" that you can use in Linux to get the same effect as Sleep if you want millisecond (or thereabouts) precision.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    avoid and replace it with..?

    no no no XD
    I meant diffrent computers not diffrent operation systems (for example a slow one and a faster one - the outcome will be diffrent [right?])
    gavra.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding delay in the connection
    By byatin in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-19-2008, 02:59 PM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. Networking (queuing delay, avg packet loss)
    By spoon_ in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-05-2005, 11:23 AM
  4. I don't see a delay() in c++!
    By Makoy in forum C++ Programming
    Replies: 16
    Last Post: 12-25-2004, 08:21 AM
  5. Delay
    By CanadianOutlaw in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:28 AM