sleep
sleep(int $seconds): int
Pauses the script for a number of seconds (blocking the current execution flow).
C C23
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main(void) {
time_t start = time(NULL);
sleep(1); /* POSIX sleep: whole seconds; nanosleep for finer units */
long elapsed = (long)(time(NULL) - start); /* second-resolution delta */
printf("%s\n", (elapsed >= 1) ? "slept at least 1s" : "unexpected");
return 0;
}
slept at least 1s
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::seconds(1)); // unit-tagged, standard library
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start).count();
std::cout << ((ms >= 1000) ? "slept at least 1s" : "unexpected") << "\n";
return 0;
}
slept at least 1s
C# v11.0.100
var start = System.Diagnostics.Stopwatch.StartNew();
Thread.Sleep(1000); // unit: milliseconds (1 second)
long ms = start.ElapsedMilliseconds;
Console.WriteLine(ms >= 1000 ? "slept at least 1s" : "unexpected");
slept at least 1s
Go v1.27.4
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
time.Sleep(time.Second) // unit-tagged Duration
ms := time.Since(start).Milliseconds()
if ms >= 1000 {
fmt.Println("slept at least 1s")
} else {
fmt.Println("unexpected")
}
}
slept at least 1s
Java v27.0.1
public class Main {
public static void main(String[] args) throws InterruptedException {
long start = System.nanoTime(); // monotonic nanosecond clock
Thread.sleep(1000); // unit: milliseconds (1 second)
long ms = (System.nanoTime() - start) / 1_000_000;
System.out.println(ms >= 1000 ? "slept at least 1s" : "unexpected");
}
}
slept at least 1s
JavaScript v26.13.1
// no blocking sleep: timers are asynchronous, unit is milliseconds
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
(async () => {
const start = Date.now();
await sleep(1000);
const ms = Date.now() - start;
console.log(ms >= 1000 ? "slept at least 1s" : "unexpected");
})();
slept at least 1s
PHP v8.6.0
$start = microtime(true);
sleep(1); // whole seconds only; usleep() = microseconds, time_nanosleep() = sec + nanosec
$ms = (microtime(true) - $start) * 1000;
echo ($ms >= 1000) ? 'slept at least 1s' : 'unexpected';
slept at least 1s
Python v3.15.2
import time
start = time.monotonic() # monotonic clock: the right tool for measuring durations
time.sleep(1) # unit: seconds; a float gives sub-second precision
ms = (time.monotonic() - start) * 1000
print("slept at least 1s" if ms >= 1000 else "unexpected")
slept at least 1s
Rust v1.99.1
use std::thread;
use std::time::{Duration, Instant};
fn main() {
let start = Instant::now(); // monotonic
thread::sleep(Duration::from_secs(1)); // unit-tagged Duration
let ms = start.elapsed().as_millis();
println!("{}", if ms >= 1000 { "slept at least 1s" } else { "unexpected" });
}
slept at least 1s
TypeScript v26.13.1
// no blocking sleep: timers are asynchronous, unit is milliseconds
const sleep = (ms: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, ms));
(async () => {
const start = Date.now();
await sleep(1000);
const ms = Date.now() - start;
console.log(ms >= 1000 ? "slept at least 1s" : "unexpected");
})();
slept at least 1s
Language differences
C: POSIX sleep (seconds) / usleep (microseconds, deprecated) / nanosleep (sec + nsec) in three grades (the example uses sleep); the example measures the before/after time() delta and asserts it lands in [1, 2) seconds — whole-second resolution suffices here, switch to clock_gettime(CLOCK_MONOTONIC) for precise timing; interruption by a signal returns early — check the remaining time.
C++: std::this_thread::sleep_for (example) is the only standard-library way, with units embedded in chrono types; the example measures the delta with steady_clock (monotonic) — never measure durations with wall-clock system_clock.
C#: Thread.Sleep(1000) is in milliseconds (example); the example measures the delta with Stopwatch (monotonic); async code uses await Task.Delay(ms) instead of blocking a thread.
Go: time.Sleep takes a Duration (example); the example gets the delta straight from time.Since(start) with no manual unit conversion; only the current goroutine sleeps while the rest of the program keeps running.
Java: Thread.sleep(1000) is milliseconds + must handle InterruptedException (example); the example measures the delta with System.nanoTime() (monotonic nanosecond clock); the modern equivalent TimeUnit.SECONDS.sleep(1) reads better.
JavaScript: the single-threaded event loop has no blocking sleep (the example wraps setTimeout in a Promise); the example measures the delta with Date.now() (wall clock, affected by system time changes) — use performance.now() for precision; the whole process keeps doing IO while awaiting.
PHP: sleep takes whole seconds only (example), sub-second needs usleep (microseconds) or time_nanosleep; the example measures the delta with microtime(true) and asserts it lands in [1, 2) seconds; under web SAPIs a long sleep pins an FPM worker — watch your timeout settings.
Python: time.sleep takes float seconds (per the example comment) — the only API of the three that is second-denominated yet accepts sub-second values; the example measures the delta with time.monotonic() (officially recommended for durations, immune to clock changes); when interrupted by a signal Python retries until the full duration.
Rust: thread::sleep(Duration) (example), units embedded in Duration; the example measures the delta with Instant::now()/elapsed() (monotonic); async ecosystems (tokio) require tokio::time::sleep or you block the whole executor.
TypeScript: same as JavaScript; the sleep(ms) helper in the example is the community-standard spelling, and await sleep(1000) reads best; the "assertion-style output" is this site's fixed idiom for demonstrating nondeterministic behavior — the output is a fixed verdict.