Delay-Wait-Sleep tricks.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
einstein1969
Expert
Posts: 763
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Delay-Wait-Sleep tricks.

#61 Post by einstein1969 » 01 Jul 2014 19:55

Squashman wrote:
einstein1969 wrote:But try to keep it as low as possible because it uses a lot of resources the pc (if exaggerated)

Wasn't the original purpose of this thread to not use a lot of resources. I remember getting yelled at for my contribution.


The goal has remained the same. It is a low CPU usage heartbeat. My suggestion is for understand how the mechanism works and how to choose the tuning parameter to use little CPU and other resource. For resources mean number of processes in the specific case of second parameter and if choose stupid exaggerated value you waste cpu.

If you try you will see that "a lot" is relative. Your method use CPU and full one core for wait. On monocore is 100% cpu.

This method use the PING sleep that not use CPU for wait. The result is about 10-20% of singol core usage for repeat consecutive sleep.
If use your method for repeat consecutive sleep you can see 100% of one core cpu usage (if AFFINITY it used). For monocore this is more easy to see.

einstein1969

dbenham
Expert
Posts: 2283
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Delay-Wait-Sleep tricks.

#62 Post by dbenham » 28 Apr 2015 08:25

I've posted a hybrid JScript/batch SLEEP.BAT that conveniently solves the problem: viewtopic.php?f=3&t=6421


Dave Benham

npocmaka_
Posts: 481
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Delay-Wait-Sleep tricks.

#63 Post by npocmaka_ » 22 Oct 2015 10:49

Two more ways

typeperf "\System\Processor Queue Length" -si 5 -sc 1 >nul

(the worse variant I think)
w32tm /stripchart /computer:localhost /period:5 /dataonly /samples:2 1>nul

Jer
Posts: 154
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Delay-Wait-Sleep tricks.

#64 Post by Jer » 27 Oct 2015 00:54

The batch code below is presented as a curiosity. It shows that computers
are not perfect for delays, but just good enough if you don't have to
worry about gaining a few extra centiseconds in the delay.

The code writes a VBScript file and calls it to produce the delay.

If you enter 500 500 500 500 at the prompt, you should see differences in
the centiseconds.

I've gone through this whole post and I am assuming that no one found
a method to get consistent and exact delays...or did I miss it?

Thanks to Aacini for posting code on SO, which I used for converting the time to milliseconds.

Code: Select all

@Echo Off
Setlocal EnableDelayedExpansion

Echo Option Explicit>timedelay.vbs
Echo Dim WshArgs>>timedelay.vbs
Echo Set WshArgs = WScript.Arguments>>timedelay.vbs
Echo WSCRIPT.SLEEP WshArgs.Item(0)>>timedelay.vbs

Echo Display starting time, ending time and delay time after entering milliseconds
Echo to send as arguments to VBScript that will use the WSCRIPT.SLEEP
Echo command to produce a delay. You should see that centiseconds can vary for
Echo the same number of milliseconds entered multiple times.&Echo(
Echo Your entry may be more than one numeric value separated by a space or comma.&Echo(
Echo Just press Enter to quit.&Echo(

:top
Set "rsp="
Set /p rsp=^>^>^>
If "%rsp%"=="" Exit /b
Set "rsp=%rsp: =,%"
Set "rsp=%rsp:,,=,%"

For %%n In (%rsp%) Do (
   Call:parseNumeric "%%n" isValid
   If "!isValid!"=="TRUE" (
      Set "startTime=!time!"
      start /wait wscript.exe //b timedelay.vbs %%n
      Set "endTime=!time!"
      rem Get elapsed time:
      Call:convertTime "!startTime!" msStart
      Call:convertTime "!endTime!" msEnd
      Set /A elapsed=msEnd-msStart
      Echo !startTime! - !endTime! - !elapsed!
   )
)

GoTo:top

::_______________________________________functions_____________________________________
:parseNumeric
 rem return TRUE if %1 contains only numeric characters, otherwise return FALSE
 setlocal EnableDelayedExpansion

 Set "temp=%~1"

 :pnLoop
 Set "char=%temp:~0,1%"
 Set "isValid=FALSE"

 For %%c In (0 1 2 3 4 5 6 7 8 9) Do If %%c==!char! (Set "isValid=TRUE"&GoTo breakPN)
 :breakPN

 Set "temp=%temp:~1%
 If "%temp%"=="" GoTo:endPNLoop
 GoTo:pnLoop

 :endPNLoop

 endlocal&set "%~2=%isValid%"&exit /b

:: *  *  *  *  *  *  *  *  *   *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
:convertTime
 rem return the time in milliseconds
 Setlocal EnableDelayedExpansion
 Set "_time=%~1"
 For /F "tokens=1-4 delims=:.," %%a In ("%_time%") Do (
    Set /A "currTime=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
 )

 endlocal&set "%2=%currTime%"&exit /b

Squashman
Expert
Posts: 4111
Joined: 23 Dec 2011 13:59

Re: Delay-Wait-Sleep tricks.

#65 Post by Squashman » 27 Oct 2015 06:36

Jer,
Not sure if it would save a few centi seconds to hybrid the script or not instead of creating the VBS script each time.

Post Reply