Thread: time.h & Delay

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sleep in windows will "delay" your program for X milliseconds - as long as your system clock is working correct, it will be precise, no matter what speed your system is (within a few milliseconds assuming no other application loading the system - if your system is heavily loaded, no delay operation is guaranteed anyways). The same applies to the corresponding functions in Linux.

    --
    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.

  2. #17
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Fine, so what should I do if I want to "delay" my program for X milliseconds (I want it to be precise) no matter if my system clock is working correct?
    gavra.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    Fine, so what should I do if I want to "delay" my program for X milliseconds (I want it to be precise) no matter if my system clock is working correct?
    What do you mean "no matter if my system clock is working correct" - if you have broken hardware, it's not going to be reliable. Period.

    --
    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.

  4. #19
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    XD
    hum I checked 2 computers to see how many clocks they do for 1 sec' and the results were diffrent - ?!
    gavra.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    XD
    hum I checked 2 computers to see how many clocks they do for 1 sec' and the results were diffrent - ?!
    What do you mean? You mean how many loops of "time2 = clock()" they will do - yes, sure, that will vary from one system to another depending on CPU speed. However, this:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <windows.h>
    
    int main()
    {
       clock_t t1 = clock(), t2;
       Sleep(1000);
       t2 = clock()-t1;
       printf("time=%d\n", (int)t2);
       return 0;
    }
    should give you the same number on different machines, because your system is ticking at the same rate.

    --
    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.

  6. #21
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    1000 I guess it's the same.
    when I do something similar I got 907..
    gavra.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    1000 I guess it's the same.
    when I do something similar I got 907..
    Post your code.

    But I would also point out that you can not expect the timing in any non-Real-Time-OS to be precise [and RTOS's are only precise if you follow certain rules, e.g. higher priority threads would dictate the real-time behaviour of low priority tasks].

    --
    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. #23
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Code:
    #include <time.h>
    #include <stdio.h>
    
    int main(void)
    {
      clock_t ticks1 = clock(), ticks2 = ticks1;
    
      while((ticks2/CLOCKS_PER_SEC-ticks1/CLOCKS_PER_SEC) < 1)
        ticks2=clock();
        
      printf("Took &#37;d ticks to wait one second.\n",ticks2-ticks1);
      
      getchar();
      return 0;
    }
    gavra.

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't seem to be able to reproduce it. I rewrote your program to run in an infinite loop and track the min/max values, like this:

    Code:
    	
    #include <time.h>
    #include <stdio.h>
    
    int main(void)
    {
        int minT = 10000, maxT = 0;
        int t = 0;
        do {
    	clock_t ticks1 = clock(), ticks2 = ticks1;
    	    
    	while((ticks2/CLOCKS_PER_SEC-ticks1/CLOCKS_PER_SEC) < 1)
    	    ticks2=clock();
    	t = ticks2 - ticks1;
    	if (minT > t) minT = t;
    	if (maxT < t) maxT = t;
    	printf("Took %d ticks to wait one second (%d, %d, min=%d, max=%d).\n",t, (int)ticks1, (int)ticks2, minT, maxT);
      } while(1);
      return 0;
    }
    The first time, I get a start of 15 and end of 1015.

    --
    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.

  10. #25
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    ohh cool it starts with 907 and then all the next get to 1000 - why? XD
    gavra.

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    ohh cool it starts with 907 and then all the next get to 1000 - why? XD
    Don't know. I'll play with it a bit and see if I can figure something out.

    --
    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.

  12. #27
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    O:
    Ok if this thread won't be here PM me.
    gavra.

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    O:
    Ok if this thread won't be here PM me.
    I still cant get it BELOW 1015 for the first iteration, but I beleive this is more accurate as it doesn't throw away any of the bits in the input number:

    Code:
    	clock_t ticks1 = clock(), ticks2 = ticks1;
    	clock_t target = 1 * CLOCKS_PER_SEC;
    	    
    	while(ticks2-ticks1 < target)
    	    ticks2=clock();
    --
    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.

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