hrtime
hrtime(bool $as_number = false): array|int
High-resolution monotonic timing: hrtime() returns a [seconds, nanoseconds] array and hrtime(true) a plain nanosecond integer. A monotonic clock only advances - NTP adjustments and DST touch the wall clock only, so use this for timing code and microtime for timestamps.
C C23
#include <stdio.h>
#include <time.h>
int main(void) {
/* clock_gettime(CLOCK_MONOTONIC) is the POSIX original; wall time would be CLOCK_REALTIME */
struct timespec a, b;
clock_gettime(CLOCK_MONOTONIC, &a);
clock_gettime(CLOCK_MONOTONIC, &b);
while (b.tv_sec < a.tv_sec || (b.tv_sec == a.tv_sec && b.tv_nsec <= a.tv_nsec)) {
clock_gettime(CLOCK_MONOTONIC, &b); /* spin until the counter strictly advances */
}
long long diff = (long long)(b.tv_sec - a.tv_sec) * 1000000000LL + (b.tv_nsec - a.tv_nsec);
printf("diff>0: %s\n", diff > 0 ? "true" : "false");
printf("ns in range: %s\n", b.tv_nsec >= 0 && b.tv_nsec < 1000000000L ? "true" : "false");
return 0;
}
diff>0: true ns in range: true
C++ C++26
#include <chrono>
#include <cstdio>
int main() {
// steady_clock is the monotonic clock; system_clock is wall time
auto a = std::chrono::steady_clock::now();
auto b = std::chrono::steady_clock::now();
while (b <= a) {
b = std::chrono::steady_clock::now(); // spin until the counter strictly advances
}
auto diff = std::chrono::duration_cast<std::chrono::nanoseconds>(b - a).count();
std::printf("diff>0: %s\n", diff > 0 ? "true" : "false");
std::printf("is steady: %s\n", std::chrono::steady_clock::is_steady ? "true" : "false");
}
diff>0: true is steady: true
C# v11.0.100
// Stopwatch.GetTimestamp reads the monotonic counter; DateTime.Now is wall time
using System.Diagnostics;
long a = Stopwatch.GetTimestamp();
long b = Stopwatch.GetTimestamp();
while (b <= a) {
b = Stopwatch.GetTimestamp(); // spin until the counter strictly advances
}
Console.WriteLine($"diff>0: {b - a > 0}");
Console.WriteLine($"frequency>0: {Stopwatch.Frequency > 0}");
diff>0: True frequency>0: True
Go v1.27.4
package main
import (
"fmt"
"time"
)
func main() {
// time.Now embeds a monotonic clock: Sub/Since never step back on NTP/DST jumps
start := time.Now()
for time.Since(start) == 0 {
// spin until the monotonic counter strictly advances
}
el := time.Since(start)
fmt.Println("elapsed>0:", el > 0)
fmt.Println("nanos>0:", el.Nanoseconds() > 0)
}
elapsed>0: true nanos>0: true
Java v27.0.1
public class Main {
public static void main(String[] args) {
// System.nanoTime() is the monotonic clock; currentTimeMillis() is wall time
long a = System.nanoTime();
long b = System.nanoTime();
while (b <= a) {
b = System.nanoTime(); // spin until the counter strictly advances
}
System.out.println("diff>0: " + (b - a > 0));
System.out.println("non-negative: " + (b >= 0));
}
}
diff>0: true non-negative: true
JavaScript v26.13.1
// performance.now() is a monotonic millisecond float; Node also has process.hrtime
let x = performance.now();
while (performance.now() <= x) {
// spin until the clock strictly advances
}
console.log(`performance.now diff>0: ${performance.now() - x > 0}`);
performance.now diff>0: true
PHP v8.6.0
// hrtime(true) is a monotonic nanosecond counter - NTP/DST adjustments only touch the wall clock
$a = hrtime(true);
$b = hrtime(true);
while ($b <= $a) { $b = hrtime(true); } // spin until the counter strictly advances
echo "diff>0: ", var_export($b - $a > 0, true), PHP_EOL;
echo "is int: ", var_export(is_int($b), true), PHP_EOL;
$pair = hrtime(); // [seconds, nanoseconds] since an arbitrary epoch
echo "array ns in range: ", var_export($pair[1] >= 0 && $pair[1] < 1_000_000_000, true), PHP_EOL;
diff>0: true is int: true array ns in range: true
Python v3.15.2
import time
# perf_counter_ns is the monotonic nanosecond counter (CLOCK_MONOTONIC on POSIX)
a = time.perf_counter_ns()
b = time.perf_counter_ns()
while b <= a:
b = time.perf_counter_ns() # spin until the counter strictly advances
print(f"diff>0: {b - a > 0}")
print(f"is int: {isinstance(b, int)}")
diff>0: True is int: True
Rust v1.99.1
use std::time::Instant;
fn main() {
// Instant is the monotonic clock; SystemTime is wall time (NTP/DST can step it back)
let t0 = Instant::now();
while t0.elapsed().as_nanos() == 0 {
// spin until the counter strictly advances
}
let el = t0.elapsed();
println!("elapsed>0: {}", el.as_nanos() > 0);
println!("subsec ns in range: {}", el.subsec_nanos() < 1_000_000_000);
}
elapsed>0: true subsec ns in range: true
TypeScript v26.13.1
// performance.now() is a monotonic millisecond float; Node also has process.hrtime
let x: number = performance.now();
while (performance.now() <= x) {
// spin until the clock strictly advances
}
const diff: number = performance.now() - x;
console.log(`performance.now diff>0: ${diff > 0}`);
performance.now diff>0: true
Language differences
C: clock_gettime(CLOCK_MONOTONIC) (<time.h>) is the prototype behind HRTime; CLOCK_MONOTONIC_RAW refuses even NTP slewing; CLOCK_REALTIME is the wall clock and can step backward after a sync.
C++: std::chrono::steady_clock is monotonic (is_steady == true is a compile-time guarantee) while system_clock tracks wall time; convert with duration_cast.
C#: Stopwatch wraps the QPC monotonic counter and Stopwatch.Frequency is its tick rate; DateTime.Now/UtcNow are wall clocks that jump on NTP adjustments.
Go: the Time from time.Now embeds a monotonic reading - Sub/Since/Until survive clock adjustments inside one process; the monotonic part is lost once a timestamp is serialized across processes.
Java: System.nanoTime() is monotonic nanoseconds and currentTimeMillis() is wall-clock milliseconds - the two are not interconvertible; jsr310's Instant.now() precision depends on the OS.
JavaScript: performance.now() is a monotonic float in milliseconds (available in browsers and Node, same origin as perf_hooks); Date.now() is wall-clock milliseconds; Node additionally offers process.hrtime.bigint() nanoseconds.
PHP: hrtime() returns a [seconds, nanoseconds] array and hrtime(true) a plain nanosecond integer (7.3+); pair it with microtime(true) - wall-clock instants use microtime, code-interval timing always uses hrtime (immune to NTP/DST jumps); HRTime\PerformanceCounter is the object-oriented wrapper with the same power.
Python: time.perf_counter_ns() (3.7+ integer nanoseconds, no float precision loss) and perf_counter(); time.monotonic() shares the source; time.time() is the wall clock.
Rust: Instant is monotonic and SystemTime is wall time; Instant::now().elapsed() is the canonical HRTime usage; wall-clock date formatting needs chrono.
TypeScript: same as JavaScript; performance exists both in the DOM lib and as a Node global.