Announcement

Collapse
No announcement yet.

sleep statement, timers, with

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • sleep statement, timers, with

    hi
    i have been using the sleep statement a lot lately.
    there was a posting in just previously posting about timers and measuring the sleep time.

    i do not know much at all about timers and measuring time, what timing routine does best for what, just basically confusion on a beginners part.

    i believe the below timer code can from David Roberts and altered by John Gleason.

    could somebody shed some light on what all this means.
    i am referring to the sleep statement and other timer functions in general.

    i wrote this program to see what the timer function would show after some sleep statement.

    because windows is a multitasking system, a program in many situations is going to have to deal with timers of sorts.

    thanks in advance for any responses
    paul


    Code:
    'pb 4.04
    '
    #COMPILE EXE
    #DIM ALL
    DECLARE FUNCTION QueryPerformanceCounter LIB "KERNEL32.DLL" ALIAS "QueryPerformanceCounter" (lpPerformanceCount AS QUAD) AS LONG
    DECLARE FUNCTION QueryPerformanceFrequency LIB "KERNEL32.DLL" ALIAS "QueryPerformanceFrequency" (lpFrequency AS QUAD) AS LONG
    
    '~~~~~~~~~~~A Variation of Dave Roberts' MACRO Timer~~~~~~~~~~~~~~~~~~~~~~~
    MACRO onTimer
      LOCAL qFreq, qOverhead, qStart, qStop AS QUAD
      LOCAL f AS STRING
      f = "#.###"
      QueryPerformanceFrequency qFreq
      QueryPerformanceCounter qStart ' Intel suggestion. First use may be suspect
      QueryPerformanceCounter qStart ' So, wack it twice <smile>
      QueryPerformanceCounter qStop
      qOverhead = qStop - qStart     ' Relatively small
    END MACRO
    
    MACRO goTimer = QueryPerformanceCounter qStart
    MACRO stopTimer = QueryPerformanceCounter qStop
    
    MACRO showTimer = USING$(f,(qStop - qStart - qOverhead)*1000000/qFreq /1000) + " milliseconds"
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    FUNCTION PBMAIN () AS LONG
    LOCAL I AS LONG
    
    ' i used the sleep statement just before its use in an effort to try keep the cpu instructions of sleep in the cpu cache
      onTimer
      gotimer
    SLEEP 1
      stoptimer
    FOR i=0 TO 22 STEP 1
    SLEEP 1
     gotimer
      SLEEP i
      stoptimer
      STDOUT "sleep "+STR$(I)+" = "+showtimer
    NEXT I
    STDOUT "press any key to continue
    WAITKEY$
    SLEEP 1
    FOR i=22 TO 45 STEP 1
    SLEEP 1
     gotimer
      SLEEP i
      stoptimer
      STDOUT "sleep "+STR$(I)+" = "+showtimer
    NEXT I
    STDOUT "press any key to continue
    WAITKEY$
    SLEEP 1
    FOR i=46 TO 69 STEP 1
    SLEEP 1
     gotimer
      SLEEP i
      stoptimer
      STDOUT "sleep "+STR$(I)+" = "+showtimer
    NEXT I
    STDOUT "press any key to continue
    WAITKEY$
    
    
    FOR i=0 TO 300 STEP 10
    SLEEP 1
     gotimer
      SLEEP i
      stoptimer
      STDOUT "sleep "+STR$(I)+" = "+showtimer
    NEXT I
     STDOUT "press any key to continue
     WAITKEY$
    END FUNCTION
    here are my results on my laptop with windows 2000 pro.
    Code:
    sorry, not code just wanted scroll bars for viewing purposes
    
    sleep  0 = 0.003 milliseconds
    sleep  1 = 15.605 milliseconds
    sleep  2 = 15.614 milliseconds
    sleep  3 = 15.605 milliseconds
    sleep  4 = 15.618 milliseconds
    sleep  5 = 15.630 milliseconds
    sleep  6 = 15.629 milliseconds
    sleep  7 = 15.426 milliseconds
    sleep  8 = 15.618 milliseconds
    sleep  9 = 15.608 milliseconds
    sleep  10 = 15.617 milliseconds
    sleep  11 = 16.177 milliseconds
    sleep  12 = 15.617 milliseconds
    sleep  13 = 16.287 milliseconds
    sleep  14 = 15.932 milliseconds
    sleep  15 = 15.619 milliseconds
    sleep  16 = 31.251 milliseconds
    sleep  17 = 31.253 milliseconds
    sleep  18 = 31.305 milliseconds
    sleep  19 = 31.243 milliseconds
    sleep  20 = 32.164 milliseconds
    sleep  21 = 31.244 milliseconds
    sleep  22 = 31.236 milliseconds
    press any key to continue
    sleep  22 = 31.247 milliseconds
    sleep  23 = 31.267 milliseconds
    sleep  24 = 31.747 milliseconds
    sleep  25 = 31.247 milliseconds
    sleep  26 = 31.238 milliseconds
    sleep  27 = 31.309 milliseconds
    sleep  28 = 31.246 milliseconds
    sleep  29 = 31.260 milliseconds
    sleep  30 = 31.237 milliseconds
    sleep  31 = 31.267 milliseconds
    sleep  32 = 46.868 milliseconds
    sleep  33 = 46.431 milliseconds
    sleep  34 = 46.871 milliseconds
    sleep  35 = 46.883 milliseconds
    sleep  36 = 46.867 milliseconds
    sleep  37 = 46.874 milliseconds
    sleep  38 = 46.885 milliseconds
    sleep  39 = 46.866 milliseconds
    sleep  40 = 46.875 milliseconds
    sleep  41 = 46.651 milliseconds
    sleep  42 = 46.877 milliseconds
    sleep  43 = 46.867 milliseconds
    sleep  44 = 46.929 milliseconds
    sleep  45 = 46.884 milliseconds
    press any key to continue
    sleep  46 = 46.874 milliseconds
    sleep  47 = 62.472 milliseconds
    sleep  48 = 62.495 milliseconds
    sleep  49 = 62.425 milliseconds
    sleep  50 = 62.473 milliseconds
    sleep  51 = 62.504 milliseconds
    sleep  52 = 62.516 milliseconds
    sleep  53 = 62.496 milliseconds
    sleep  54 = 62.493 milliseconds
    sleep  55 = 62.489 milliseconds
    sleep  56 = 62.530 milliseconds
    sleep  57 = 62.962 milliseconds
    sleep  58 = 62.557 milliseconds
    sleep  59 = 62.506 milliseconds
    sleep  60 = 62.514 milliseconds
    sleep  61 = 62.584 milliseconds
    sleep  62 = 62.508 milliseconds
    sleep  63 = 78.038 milliseconds
    sleep  64 = 78.112 milliseconds
    sleep  65 = 78.126 milliseconds
    sleep  66 = 78.119 milliseconds
    sleep  67 = 78.119 milliseconds
    sleep  68 = 78.109 milliseconds
    sleep  69 = 78.121 milliseconds
    press any key to continue
    sleep  0 = 0.083 milliseconds
    sleep  10 = 15.914 milliseconds
    sleep  20 = 31.223 milliseconds
    sleep  30 = 31.244 milliseconds
    sleep  40 = 46.896 milliseconds
    sleep  50 = 62.497 milliseconds
    sleep  60 = 62.519 milliseconds
    sleep  70 = 78.117 milliseconds
    sleep  80 = 93.947 milliseconds
    sleep  90 = 94.271 milliseconds
    sleep  100 = 109.356 milliseconds
    sleep  110 = 125.008 milliseconds
    sleep  120 = 125.013 milliseconds
    sleep  130 = 140.628 milliseconds
    sleep  140 = 140.690 milliseconds
    sleep  150 = 156.266 milliseconds
    sleep  160 = 171.897 milliseconds
    sleep  170 = 171.923 milliseconds
    sleep  180 = 187.430 milliseconds
    sleep  190 = 203.138 milliseconds
    sleep  200 = 203.129 milliseconds
    sleep  210 = 220.623 milliseconds
    sleep  220 = 234.400 milliseconds
    sleep  230 = 234.416 milliseconds
    sleep  240 = 250.019 milliseconds
    sleep  250 = 249.998 milliseconds
    sleep  260 = 265.645 milliseconds
    sleep  270 = 280.569 milliseconds
    sleep  280 = 281.232 milliseconds
    sleep  290 = 296.889 milliseconds
    sleep  300 = 312.524 milliseconds
    press any key to continue
    Last edited by Paul Purvis; 29 Mar 2008, 03:30 PM.
    p purvis

  • #2
    Your test is not a good one Paul.

    For code timing purposes we should do several runs and then average them - there is far too much going on to make a meaningful judgement on single runs. Your code is doing many single runs.

    We have quite a few timing methods available to us with varying degrees of granularity but the one which most agree is the best of the bunch is QueryPerformanceCounter. The macro, obviously, just wraps the method into an easy to use tool.

    Since our PCs are synchronous computers they do not accommodate precision timing exercises. For that we need an asynchronous computer. You can get one built but be prepared to sell your car, house and whatever else you may possess.
    Last edited by David Roberts; 29 Mar 2008, 03:04 PM. Reason: My keyboard is throwing up hh instead of h!

    Comment


    • #3
      Have a look here:
      http://www.powerbasic.com/support/pb...5&postcount=15

      Paul.

      Comment


      • #4
        thanks David for your quick reply, i was hoping you replied.
        thanks for posting of your code

        i see that i made an error of John's name, i going to fix that, sorry John Gleason.

        one observation i did not make in the beginning is that it seems the milliseconds on my test are incremented every 15 milliseconds when there is a significant change in times returned.

        after reviewing my results it would maybe seem prudent to have a routine to test the timing of the sleep statement with the results in an array, then use the array's matrix when applying the sleep statement, especially when programs never run on just the same computer and computers and os vary as was mentioned in the pb's documentation of the sleep statement.

        i am very glad the previous posting
        http://powerbasic.com/support/pbforu...ad.php?t=36930
        brought up mention of the sleep statement and the measuring of times.
        i am doing some coding where i need to measurement the time of my routines and see what is going on inside my program(tcp stuff).

        paul
        p purvis

        Comment


        • #5
          Paul (Purvis), here are my laptop results, win98SE. Overall pretty accurate, and much different than your data:
          Code:
          sleep  0 = 0.005 milliseconds
          sleep  1 = 1.436 milliseconds
          sleep  2 = 1.406 milliseconds
          sleep  3 = 3.131 milliseconds
          sleep  4 = 3.363 milliseconds
          sleep  5 = 5.031 milliseconds
          sleep  6 = 6.155 milliseconds
          sleep  7 = 7.169 milliseconds
          sleep  8 = 9.082 milliseconds
          sleep  9 = 8.349 milliseconds
          sleep  10 = 10.003 milliseconds
          sleep  11 = 11.104 milliseconds
          sleep  12 = 11.984 milliseconds
          sleep  13 = 14.155 milliseconds
          sleep  14 = 13.362 milliseconds
          sleep  15 = 14.980 milliseconds
          sleep  16 = 16.310 milliseconds
          sleep  17 = 17.809 milliseconds
          sleep  18 = 17.995 milliseconds
          sleep  19 = 19.137 milliseconds
          sleep  20 = 20.973 milliseconds
          sleep  21 = 23.922 milliseconds
          sleep  22 = 22.843 milliseconds
          sleep  22 = 22.876 milliseconds
          sleep  23 = 23.857 milliseconds
          sleep  24 = 24.125 milliseconds
          sleep  25 = 25.973 milliseconds
          sleep  26 = 26.869 milliseconds
          sleep  27 = 27.003 milliseconds
          sleep  28 = 27.365 milliseconds
          sleep  29 = 29.989 milliseconds
          sleep  30 = 30.026 milliseconds
          sleep  31 = 32.065 milliseconds
          sleep  32 = 32.439 milliseconds
          sleep  33 = 33.826 milliseconds
          sleep  34 = 34.955 milliseconds
          sleep  35 = 35.960 milliseconds
          sleep  36 = 36.875 milliseconds
          sleep  37 = 37.061 milliseconds
          sleep  38 = 37.381 milliseconds
          sleep  39 = 38.388 milliseconds
          sleep  40 = 39.977 milliseconds
          sleep  41 = 41.140 milliseconds
          sleep  42 = 41.992 milliseconds
          sleep  43 = 43.118 milliseconds
          sleep  44 = 44.291 milliseconds
          sleep  45 = 44.999 milliseconds
          sleep  46 = 47.025 milliseconds
          sleep  47 = 46.990 milliseconds
          sleep  48 = 48.010 milliseconds
          sleep  49 = 49.097 milliseconds
          sleep  50 = 49.989 milliseconds
          sleep  51 = 51.100 milliseconds
          sleep  52 = 51.049 milliseconds
          sleep  53 = 53.871 milliseconds
          sleep  54 = 54.118 milliseconds
          sleep  55 = 54.992 milliseconds
          sleep  56 = 56.492 milliseconds
          sleep  57 = 56.951 milliseconds
          sleep  58 = 58.011 milliseconds
          sleep  59 = 59.114 milliseconds
          sleep  60 = 59.966 milliseconds
          sleep  61 = 61.089 milliseconds
          sleep  62 = 62.010 milliseconds
          sleep  63 = 62.993 milliseconds
          sleep  64 = 63.165 milliseconds
          sleep  65 = 64.719 milliseconds
          sleep  66 = 66.018 milliseconds
          sleep  67 = 67.872 milliseconds
          sleep  68 = 67.894 milliseconds
          sleep  69 = 70.037 milliseconds
          sleep  0 = 16.278 milliseconds
          sleep  10 = 9.978 milliseconds
          sleep  20 = 20.929 milliseconds
          sleep  30 = 31.117 milliseconds
          sleep  40 = 39.997 milliseconds
          sleep  50 = 50.001 milliseconds
          sleep  60 = 59.361 milliseconds
          sleep  70 = 69.996 milliseconds
          sleep  80 = 80.169 milliseconds
          sleep  90 = 90.909 milliseconds
          sleep  100 = 99.985 milliseconds
          sleep  110 = 111.008 milliseconds
          sleep  120 = 120.050 milliseconds
          sleep  130 = 130.031 milliseconds
          sleep  140 = 139.986 milliseconds
          sleep  150 = 150.222 milliseconds
          sleep  160 = 159.948 milliseconds
          sleep  170 = 169.984 milliseconds
          sleep  180 = 179.328 milliseconds
          sleep  190 = 189.978 milliseconds
          sleep  200 = 199.992 milliseconds
          sleep  210 = 209.321 milliseconds
          sleep  220 = 219.974 milliseconds
          sleep  230 = 229.970 milliseconds
          sleep  240 = 239.370 milliseconds
          sleep  250 = 249.970 milliseconds
          sleep  260 = 259.968 milliseconds
          sleep  270 = 270.886 milliseconds
          sleep  280 = 279.970 milliseconds
          sleep  290 = 289.313 milliseconds
          sleep  300 = 303.273 milliseconds

          Comment


          • #6
            SLEEP is not a timer.

            In addition to "not resuming" for at least the named time period (rounded), SLEEP causes the O/S to give processing time to other threads.
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              My figures on XP Pro SP2 are in line with Paul P's figures.

              I got some Sleep 300 figures in the range 312ms but the average of 10 was 310.79.

              To quieten things down a lot, but still not as quiet as 98SE, I booted into Safe Mode. My figures didn't change.

              I do remember my old 98SE system did give more accurate figures when we discussed this topic about two years ago.

              I used to sync my 98 system to a time server a couple of time a week. On this system I sync every six hours because the drift is monumental.

              My 98 system saw life initially with an AMD Athlon 600 and croaked with an Athlon 900. I should add that the 900 did not croak it - I croaked it. The 600 timings were better than the 900.

              Comment


              • #8
                a big thanks for doing those test.
                John, i was surprised at those results.
                i guess that maybe shows why a lot of programs(communications,etc) sort of has problems with a windows nt based os, but i am not an expert of any kind in those areas.


                i put the program at website below for anybody wanting a quick test and i increased the testing times
                http://pdptemp.dyndns.org/zipcode

                i could not find the code to list the os cpu and other computer info or i would of had placed that in the program.
                Last edited by Paul Purvis; 29 Mar 2008, 05:02 PM.
                p purvis

                Comment


                • #9
                  I'd bet Real Money some of the "problems" associated with "inaccurate timing" and/or "different behavior different operating systems" are actually just the result of trying to use "timing" instead of synchronization objects.
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    The SDK help says,
                    Use the QueryPerformanceCounter and QueryPerformanceFrequency functions to measure short time intervals at a high resolution.
                    Michael, do you have any examples / posts showing how sync objects might be used as a high-res timer? Also, since we're only at mSec resolution with SLEEP, I'm thinking that even getTickCount might be an answer.

                    Comment


                    • #11
                      John,
                      since we're only at mSec resolution with SLEEP, I'm thinking that even getTickCount might be an answer.
                      SLEEP is using the tick to determine how long to sleep for so it's not a good idea to time the SLEEP using getTickCount. The resolution of GetTickCount will typically be 15ms, the same as the resolution of SLEEP.
                      If you want reasonably accurate timings then use the performance counters or the faster time stamp counter !RDTSC . Which to choose depends on what you're trying to time.

                      Paul.

                      Comment


                      • #12
                        Michael, do you have any examples / posts showing how sync objects might be used as a high-res timer?
                        Of course I don't, and neither does anyone else:

                        Timers are Timers. Synchronization objects are synchronization objects.

                        What do you want to do?

                        Time something? Use a timer.

                        Wait for something to happen? Use a synchronization object.

                        GetTickCount, QueryPerformanceCounter, SetTimer ===> Timers
                        THREAD CREATE, CreateEvent, Shell/CreateProcess/ShellExecuteEx, SleepEx, ReadFile==> Synchronization objects

                        Using - or rather misusing - timers as synchronization objects:
                        Code:
                        FUNCTION....
                         Do Stuff
                         DO 
                            Sleep x
                          LOOP Until GlobalVar = Somevalue  
                        
                        END FUNCTION
                        Using Synchronization object
                        Code:
                        FUNCTION  .....
                        
                          DO Stuff
                        
                          WaitForsingleObject/WaitForMultipleObjects
                        END FUNCTION
                        MCM
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          hi Michael,
                          i believe i saw you made a remark about a vacation lately and not seen any post from you for a couple of days.
                          i am sure that vacation could have been longer huh, i need a long one.

                          i am interested, could you be more clearer to on this, thanks.

                          poffs had 375 hits on this
                          could you provide a more simpler example, thanks


                          WaitForsingleObject/WaitForMultipleObjects
                          Last edited by Paul Purvis; 30 Mar 2008, 06:39 PM.
                          p purvis

                          Comment


                          • #14
                            I have multiple demos in the source code forum using synchronization objects with Windows' events, and so do others.

                            e.g
                            Terminate Worker Threads Using Windows Events (and Using Thread Local Storage) Demo Dec 23 2005

                            Waitable Timer Object Demo June 2005

                            GUI + Worker Thread + Abort Demo


                            My point was, to time something, various types of timers are provided by Windows;
                            to wait for something to happen, synchronization objects are provided.
                            Michael Mattias
                            Tal Systems (retired)
                            Port Washington WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment


                            • #15
                              thanks Michael
                              i have to look over learn some of that because it seems i am always waiting.
                              paul
                              p purvis

                              Comment

                              Working...
                              X