Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct, cross-platform way to sleep for less than a second? [duplicate]

I want my script's execution to pause for about a half of a second.

While looking at the PHP documentation's user comments, I found multiple functions that purported to be able to do this. But, for each function, there was at least one poster who claimed that it was wrong and that they had a better way of doing it, and so-on and so-forth.

Thanks,

like image 348
Chris Laplante Avatar asked Jul 20 '11 01:07

Chris Laplante


People also ask

How do you sleep in milliseconds?

To sleep for milliseconds with this method, simply use a fractional number. To sleep for 400 milliseconds, for example, use time. sleep(0.4), use time for 60 milliseconds sleep(0.06), for example. Python's sleep() function is a part of the time package.

How do you sleep time in C++?

The way to sleep your program in C++ is the Sleep(int) method. The header file for it is #include "windows. h." The time it sleeps is measured in milliseconds and has no limit.

What is the difference between sleep and Usleep?

The difference between sleep() and usleep() is that sleep() takes a number of seconds as its parameter, whereas usleep() takes a number of microseconds - millionths of a second - as its parameter.

How accurate is sleep function Python?

The accuracy of the time. sleep function depends on your underlying OS's sleep accuracy. For non-realtime OS's like a stock Windows the smallest interval you can sleep for is about 10-13ms. I have seen accurate sleeps within several milliseconds of that time when above the minimum 10-13ms.


1 Answers

The first function I would try is usleep. From the docs:

Delays program execution for the given number of micro seconds.

like image 98
Justin Ethier Avatar answered Sep 26 '22 16:09

Justin Ethier