usleep
usleep(int $microseconds): void
Delays script execution in microseconds (sub-second delays).
C C23
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main(void) {
struct timespec t0, t1;
timespec_get(&t0, TIME_UTC);
usleep(250000); /* microseconds: 250 ms (deprecated in POSIX; nanosleep is the modern pick) */
timespec_get(&t1, TIME_UTC);
long ms = (t1.tv_sec - t0.tv_sec) * 1000 + (t1.tv_nsec - t0.tv_nsec) / 1000000;
printf("%s\n", (ms >= 250) ? "slept at least 250ms" : "unexpected");
return 0;
}
slept at least 250ms
C++ C++26
#include <chrono>
#include <iostream>
#include <thread>
int main() {
auto start = std::chrono::steady_clock::now();
std::this_thread::sleep_for(std::chrono::microseconds(250000)); // microsecond units
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start).count();
std::cout << ((ms >= 250) ? "slept at least 250ms" : "unexpected") << "\n";
return 0;
}
slept at least 250ms
C# v11.0.100
var start = System.Diagnostics.Stopwatch.StartNew();
Thread.Sleep(250); // milliseconds only: no built-in microsecond sleep
long ms = start.ElapsedMilliseconds;
Console.WriteLine(ms >= 250 ? "slept at least 250ms" : "unexpected");
slept at least 250ms
Go v1.27.4
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
time.Sleep(250 * time.Millisecond) // Duration carries nanosecond units, so sub-second is native
ms := time.Since(start).Milliseconds()
if ms >= 250 {
fmt.Println("slept at least 250ms")
} else {
fmt.Println("unexpected")
}
}
slept at least 250ms
Java v27.0.1
public class Main {
public static void main(String[] args) throws InterruptedException {
long start = System.nanoTime();
Thread.sleep(250, 0); // millis + nanos overload; real granularity is still millisecond-ish
long ms = (System.nanoTime() - start) / 1_000_000;
System.out.println(ms >= 250 ? "slept at least 250ms" : "unexpected");
}
}
slept at least 250ms
JavaScript v26.13.1
// timers are asynchronous and millisecond-granular: no usleep equivalent
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
(async () => {
const start = Date.now();
await sleep(250);
const ms = Date.now() - start;
console.log(ms >= 250 ? "slept at least 250ms" : "unexpected");
})();
slept at least 250ms
PHP v8.6.0
$start = microtime(true);
usleep(250000); // 250000 microseconds = 250 ms (vs sleep: whole seconds only)
$ms = (microtime(true) - $start) * 1000;
echo ($ms >= 250) ? 'slept at least 250ms' : 'unexpected';
slept at least 250ms
Python v3.15.2
import time
start = time.monotonic()
time.sleep(0.25) # float seconds: 250 ms (the only second-denominated sub-second API here)
ms = (time.monotonic() - start) * 1000
print("slept at least 250ms" if ms >= 250 else "unexpected")
slept at least 250ms
Rust v1.99.1
use std::thread;
use std::time::{Duration, Instant};
fn main() {
let start = Instant::now();
thread::sleep(Duration::from_micros(250000)); // microsecond-denominated constructor
let ms = start.elapsed().as_millis();
println!("{}", if ms >= 250 { "slept at least 250ms" } else { "unexpected" });
}
slept at least 250ms
TypeScript v26.13.1
// timers are asynchronous and millisecond-granular: no usleep equivalent
const sleep = (ms: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, ms));
(async () => {
const start = Date.now();
await sleep(250);
const ms = Date.now() - start;
console.log(ms >= 250 ? "slept at least 250ms" : "unexpected");
})();
slept at least 250ms
Language differences
C: usleep (microseconds) is marked deprecated in POSIX — the modern pick is nanosleep (sec + nsec, per the example comment); the example measures the delta with timespec_get (C11 wall clock) and asserts it lands in [250, 1000) ms; true microsecond precision depends on the scheduler, millisecond slack is normal.
C++: std::this_thread::sleep_for(std::chrono::microseconds(...)) takes microsecond units directly (example); the same function spans nanoseconds to hours — chrono's unit types are the error-proofing design.
C#: only millisecond-granular Thread.Sleep (example) — no built-in microsecond sleep; if you truly need microseconds it is P/Invoke or SpinWait busy-waiting (the latter pins a core, use with care).
Go: time.Sleep takes a nanosecond-denominated Duration (the example uses 250 * time.Millisecond); time.Microsecond/time.Nanosecond work the same way — sub-second delays are native, no second function needed.
Java: Thread.sleep(250, 0) has a millis+nanos overload on paper (example), but OS time slices keep the real precision at millisecond level; on most platforms the nanos argument just rounds.
JavaScript: setTimeout is millisecond-granular and even less precise (nested timeouts get clamped to a 4ms minimum in browsers) — there is no usleep equivalent (the Promise wrapper in the example is the millisecond idiom); true microsecond waiting means performance.now() busy-waiting, an anti-pattern.
PHP: usleep(microseconds) differs from sleep only in unit and return value — usleep takes sub-second values and returns nothing (the example passes 250000 for 250 ms); in web contexts millisecond sleeps still pin an FPM worker, use a queue for long waits.
Python: time.sleep(0.25) with float seconds (example) — Python has no separate microsecond-sleep function, float seconds are its usleep; the underlying implementation is already high-resolution (select / clock_nanosleep), sub-millisecond drift is controlled.
Rust: Duration::from_micros(250000) is microsecond-denominated outright (example), with from_nanos/from_millis siblings — unit mix-ups are impossible at the type level.
TypeScript: same as JavaScript — millisecond-granular timers, fractional-millisecond values like sleep(0.25) get clamped, no microsecond capability; the "assertion-style output" removes the jitter of the real delay value.