çè ã¯PHPã®ç¾å¨æå»ãä¸æ¸ãããPHPæ¡å¼µã¢ã¸ã¥ã¼ã«php-timecopãéçºãã¦ãããããPHPã®æéããã¿ã®ãã¹ããä¸éä¸è¬ã®äººããå¤ãæ¸ãã¦ããã¨æãã¾ãããã¹ãã±ã¼ã¹ä¸ã§usleepé¢æ°ãå¤ç¨ãã¦ããã®ã¯ä¸çä¸ã§ãçè ãããããããã¾ããã
ã¨ããã§ãå
æ¥php-timecopã®ãã¹ããWindowsä¸ã§åãããã¨ããã usleep(100000)
ã99.8msãããã§å¸°ã£ã¦ãã¦ãã¹ãã«å¤±æããã¨ãããã¨ãããã¾ããã
çè ã¯sleepé¢æ°ãusleepé¢æ°ã¯æå®ããæéã¨åããããããé·ãæéã¹ãªã¼ãããã¨èãã¦ããã®ã§ãããæ¬å½ã«ãã®ãããªæ§è³ªãããã®ã§ããããï¼ã¾ããsleepé¢æ°ãusleepé¢æ°ã¯ã©ã®ç¨åº¦ã®èª¤å·®ãããã®ã§ããããï¼
æ¬ç¨¿ã§ã¯ããããsleepãusleepã®æåã«ã¤ãã¦æ·±æããã¦ã¿ã¾ãã
sleepé¢æ°ã®æå
ã¾ãã¯sleepé¢æ°ã®æåãã調ã¹ã¦ã¿ã¾ããããLinuxãmacOSãWindowsã®åç°å¢ã§ sleep(1)
ãã¦å¸°ã£ã¦ããã¾ã§ã®æéã1000å測å®ããã¨ãã®çµæãä¸è¨ã«ç¤ºãã¾ãã
Linuxã®å ´å
ä¸å³ãLinuxç°å¢ï¼Debian 8.7, Kernel 3.16.0, x86_64, PHP 5.6.30ï¼ã§ã®å®é¨çµæã®ãã¹ãã°ã©ã ã§ãã横軸ã®åä½ã¯usã§ãã
1.0001ç§ãããã«ãã¼ã¯ãããã®ããããã¾ããè¨ãæããã¨ç´100usã®ãºã¬ã§ãã
Windowsã®å ´å
Windowsç°å¢ï¼Windows 10, x86_64, PHP 7.1.6ï¼ã§ã®å®é¨çµæã®ãã¹ãã°ã©ã ã§ãã
ãã¼ã¯ã¯1.001ç§ãããã«ãªã£ã¦ãã¾ããè¨ãæããã¨ç´1msã®ãºã¬ã§ãã
macOSã®å ´å
macOSç°å¢ï¼Mac OS X 10.11.6, x86_64, PHP 7.1.6ï¼ã§ã®å®é¨çµæã®ãã¹ãã°ã©ã ã§ãã
ãã¼ã¯ã¯1.005ç§ãããã«ãªã£ã¦ãã¾ããè¨ãæããã¨ç´5msã®ãºã¬ã§ãã
3ç°å¢ã¨ãã1ç§ããæ©ã帰ã£ã¦ãããã®ã¯1件ãããã¾ããã§ããã
POSIXã®ä»æ§ã確èªãã
ã¡ãªã¿ã«ãPHPã®sleepé¢æ°ã¯ã©ã®ç°å¢ã§ãOS/æ¨æºã©ã¤ãã©ãªã®sleepãå¼ã³åºãã¦ãã¾ããPOSIXã®sleep()ã®manpageã«ã¯æ¬¡ã®ãããªè¨è¿°ãããã¾ãã
The sleep() function shall cause the calling thread to be suspended from execution until either the number of realtime seconds specified by the argument seconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process. The suspension time may be longer than requested due to the scheduling of other activity by the system.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/sleep.html
æå®ãããæéãçµéããã¾ã§å®è¡ãåæ¢ããããã¹ã±ã¸ã¥ã¼ãªã³ã°ã®é½åã§æå®ããããããé·ãåæ¢ãããã¨ãããããã¨ã®ãã¨ã§ããããæå®æéããæ©ã帰ã£ã¦ãããã¨ã¯ãªããã¨ãä¿è¨¼ããã¦ããããã§ãã
usleepé¢æ°ã®æå
次ã«usleepã®å¾åã調ã¹ã¦ã¿ã¾ããsleepé¢æ°ã¨åæ§ã«3ç°å¢ã§usleep(1000000)
ãã¦å¸°ã£ã¦ããã¾ã§ã®æéã測å®ãã¾ããã
Linux/macOSã®å ´å
Linuxã¨macOSã®å ´åã¯sleep(1)
ã®ã¨ãã¨å¤§å·®ãªãçµæã«ãªãã¾ãããä¸è¨ã®ãã¹ãã°ã©ã ãLinuxã®çµæã§ãã
以ä¸ã¯macOSã®çµæã§ãã
ã©ã¡ãã®ç°å¢ãsleepé¢æ°ã®ã¨ãã¨åæ§ã1ç§ããæ©ã帰ã£ã¦ãããã®ã¯1件ãããã¾ããã§ããã両OSã¨ãå é¨çã«POSIXæºæ ã®usleepãå¼ã³åºãã¦ãããããsleepé¢æ°ã®ã¨ãã¨ä¼¼ãæåãªã®ã¯å½ç¶ã¨è¨ãããã§ãã
Windowsã®å ´å
ãã¦ãWindowsã®å ´åã¯æå¤ã¨ãè¨ããçµæã«ãªãã¾ããã
ã³ãã2ã¤ããåå¸ã«ãªã£ã¦ãããæåã®ãã¼ã¯ã¯1000000nsï¼ï¼1ç§ï¼ããå ãã«åã«ãªã£ã¦ãã¾ããæ¬ç¨¿ã®æåã§Windowsä¸ã®PHPã§ã¯usleepé¢æ°ãæå®ããæéããå°ãæ©ã帰ã£ã¦ãããã¨ãããã¨ãã話ãç´¹ä»ãã¾ãããããã®éãã®çµæã«ãªã£ã¦ããããã§ãã
PHPã®Windowsç¨ã®ã½ã¼ã¹ã³ã¼ããè¦ã¦ã¿ãã¨ãusleepé¢æ°ã®å®ç¾ã«ã¯Windowsã®ãWaitable Timer Objectsããå©ç¨ããã¦ãã¾ããã¾ãããããå©ç¨ããã¨åæ¢æéãæå®ããçããªããã¨ãããããã§ãï¼ãã¨ãã°ä¸è¨ã®è¨äºãåç §ï¼ã
ã¾ã¨ã
- PHPã®usleepé¢æ°ã¯Windowsä¸ã§ã¯æå®ãããæéããçãåæ¢æéã«ãªããã¨ããã
- å
¨ã¦ã®ç°å¢ã®sleepé¢æ°ã»Linux/macOSä¸ã®usleepé¢æ°ã¯æå®ãããæéã¨åãããã以ä¸åæ¢ãã
- å é¨çã«å¼ã³åºãã¦ããã©ã¤ãã©ãªã³ã¼ã«sleepã»usleepã®ä»æ§
- æå®ãããç§æ°ããã©ãããããºã¬ããã¯OSã«ãã£ã¦ç°ãªã
usleepã¨ããé¢æ°åãªã®ã«Windowsä¸ã®æåãä»ç°å¢ã¨ç°ãªã£ã¦ããã®ã¯ãã°ã¨ã¾ã§ã¯è¨ãåãã¾ãããããã¼ã¿ããªãã£ä¸ã¯åé¡ãããããã§ãã念ã®ããPHPæ¬ä½ã«ãã°ã¬ãã¼ããåºãã¦ãããã¨æãã¾ãã
追試ç¨ã®æ å ±
ä»åã®å®é¨ã«ä½¿ã£ãPHPã¹ã¯ãªããã¯ä¸è¨ã§ããã«ã¼ãã«ã®ã³ã³ãã¤ã«ãªãã·ã§ã³ããã®ä»ã®æ¡ä»¶æ¬¡ç¬¬ã§çµæãå¤ãããã¨æãã¾ãã