Sleep: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
Line 3: Line 3:
== MS-DOS on Windows ==
== MS-DOS on Windows ==
=== Approach 1: %RANDOM% + ping local ===
=== Approach 1: %RANDOM% + ping local ===
[https://stackoverflow.com/questions/8258087/bat-random-timeout batch file - .bat random timeout - Stack Overflow]
Purpose
* {{kbd | key=<nowiki>%RANDOM%</nowiki>}} returns an integer between 0 and 32767<ref>[https://ss64.com/nt/syntax-random.html Random Numbers - Windows CMD - SS64.com]</ref>.
* Sleep random seconds between 5 ~ 65 seconds
* 60 is the range of values you want: 0 to 60.
* 32768 is the range of values returned by {{kbd | key=<nowiki>%RANDOM%</nowiki>}} (0 to 32767).
* 5 is the minimum value you want. The original range of values you want '0 to 60' became '5 ~ 65'.
* ping local address: {{kbd | key=<nowiki>/n <Count></nowiki>}} "Specifies the number of echo Request messages sent. The default is 4."<ref>[https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/ping time]</ref><ref>[https://stackoverflow.com/questions/735285/how-to-wait-in-a-batch-script windows xp - How to wait in a batch script? - Stack Overflow]</ref>


BAT file:
BAT file:
Line 22: Line 18:
ECHO %date% %time%
ECHO %date% %time%
</pre>
</pre>
Instruction<ref>[https://stackoverflow.com/questions/8258087/bat-random-timeout batch file - .bat random timeout - Stack Overflow]</ref>
* {{kbd | key=<nowiki>%RANDOM%</nowiki>}} returns an integer between 0 and 32767<ref>[https://ss64.com/nt/syntax-random.html Random Numbers - Windows CMD - SS64.com]</ref>.
* 60 is the range of values you want: 0 to 60.
* 32768 is the range of values returned by {{kbd | key=<nowiki>%RANDOM%</nowiki>}} (0 to 32767).
* 5 is the minimum value you want. The original range of values you want '0 to 60' became '5 ~ 65'.
* ping local address: {{kbd | key=<nowiki>/n <Count></nowiki>}} "Specifies the number of echo Request messages sent. The default is 4."<ref>[https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/ping time]</ref><ref>[https://stackoverflow.com/questions/735285/how-to-wait-in-a-batch-script windows xp - How to wait in a batch script? - Stack Overflow]</ref>


=== Approach 2: sleep package of CygWin ===
=== Approach 2: sleep package of CygWin ===

Revision as of 12:14, 6 February 2023

Sleep random seconds in programming

MS-DOS on Windows

Approach 1: %RANDOM% + ping local

Purpose

  • Sleep random seconds between 5 ~ 65 seconds

BAT file:

REM print current date & time
ECHO %date% %time%

REM sleep 5 ~ 65 seconds
SET /a timeout=%RANDOM% * 60 / 32768 + 5
ping 127.0.0.1 -n %timeout% > nul

REM print current date & time
ECHO %date% %time%

Instruction[1]

  • %RANDOM% returns an integer between 0 and 32767[2].
  • 60 is the range of values you want: 0 to 60.
  • 32768 is the range of values returned by %RANDOM% (0 to 32767).
  • 5 is the minimum value you want. The original range of values you want '0 to 60' became '5 ~ 65'.
  • ping local address: /n <Count> "Specifies the number of echo Request messages sent. The default is 4."[3][4]

Approach 2: sleep package of CygWin

Requirement: Install Cygwin on Win Os windows.png & install sleep package (How-to: Setting Up Cygwin/X)

Verify the installation of sleep package[5][6]

  1. Key-in cmd to open command prompt (How to Open Command Prompt (Windows 10, 8, 7, Vista, XP))
  2. Key-in one of following commands
    • Key-in where sleep if you add installation path of CygWin to system path (How to setup my system path). Or
    • Key-in dir C:\cygwin64\bin\sleep.exe if the installation path of CygWin is C:\cygwin64\.
C:\Users\user>dir C:\cygwin64\bin\sleep.exe

# successful condition
C:\cygwin64\bin\sleep.exe

# failed condition
File Not Found

BAT file:

REM print current date & time
ECHO %date% %time%

REM sleep 5 ~ 65 seconds
SET /a timeout=%RANDOM% * 60 / 32768 + 5
C:\cygwin64\bin\sleep.exe %timeout%

REM print current date & time
ECHO %date% %time%

Approach 3: TIMEOUT

Alternative command: Icon_exclaim.gif The following command ECHO %date% %time% will not be executed after TIMEOUT %timeout% was executed.

BAT file:

SET /a timeout=%RANDOM% * 60 / 32768 + 5
TIMEOUT %timeout%

ECHO %date% %time%

BASH

Approach1: sleep

Tested on Linux Os linux.png & macOS icon_os_mac.png [7][8]

# print current date & time
echo $(date '+%Y-%m-%d %H:%M:%S')

# sleep 5 ~ 65 seconds
timeout=$(($RANDOM * 60 / 32768 + 5))
sleep $timeout

# print current date & time
echo $(date '+%Y-%m-%d %H:%M:%S')

Sleep couple microseconds[9]. Tested on Linux Os linux.png

# print current unix timestamp e.g. 1598346362244
date +%s%N

# sleep 1 millisecond = 0.001 seconds
sleep 0.001s

# print current unix timestamp
date +%s%N


Sleep couple microseconds. Tested on macOS icon_os_mac.png

# print current unix timestamp e.g. 1598346362244
php -r 'echo microtime(TRUE) . PHP_EOL;'

# sleep 1 millisecond = 0.001 seconds
sleep 0.001s

# print current unix timestamp
php -r 'echo microtime(TRUE) . PHP_EOL;'


Sleep couple microseconds. Tested on macOS icon_os_mac.png

# print current unix timestamp e.g. 1598346362244
Sleep couple microseconds. Tested on {{Mac}}

<pre>
# print current unix timestamp e.g. 1598346362244
php -r 'echo microtime(TRUE) . PHP_EOL;'

# sleep 1 millisecond = 0.001 seconds
sleep 0.001s

# print current unix timestamp
php -r 'echo microtime(TRUE) . PHP_EOL;'


Sleep couple milliseconds (1 seconds = 1000 milliseconds) e.g. 1671090194.136471987[10]

# print current unix timestamp in milliseconds e.g. 1671090194.136471987
perl -MTime::HiRes=time -e 'printf "%.9f\n", time'

# sleep 1 millisecond = 0.001 seconds
sleep 0.001s

# print current unix timestamp
perl -MTime::HiRes=time -e 'printf "%.9f\n", time'

Approach2: ping

Tested on Linux Os linux.png & macOS icon_os_mac.png

ping local address: -c <Count> "Stop after sending (and receiving) count ECHO_RESPONSE packets."[11]

# print current date & time
echo $(date '+%Y-%m-%d %H:%M:%S')

# sleep 5 ~ 65 seconds
timeout=$(($RANDOM * 60 / 32768 + 5))
ping 127.0.0.1 -c $timeout > nul

# print current date & time
echo $(date '+%Y-%m-%d %H:%M:%S')

Approach3: usleep

usleep(3) - Linux man page "suspend execution for microsecond intervals" (1 second = 1 000 000 microsecond). Tested on Linux Os linux.png

# print current date, time & nanoseconds
echo $(date '+%Y-%m-%d %H:%M:%S')$(date '+.%N')

# sleep 0.2 ~ 1.2 seconds
timeout=$(($RANDOM * 1000000 / 32768 + 200000))
echo $timeout
usleep $timeout

# print current date, time & nanoseconds
echo $(date '+%Y-%m-%d %H:%M:%S')$(date '+.%N')

PHP

JavaScript

References

Related