Time Module

The Time module benchmarks the overhead of X Platform's time acquisition APIs. Time acquisition is a critical operation in high-performance systems, and this benchmark helps quantify the cost of different time sources.

Overview

X Platform provides multiple time sources optimized for different use cases:

  • Native Time: Uses native (JNI) calls for high-resolution wall-clock time

  • Epoch Time: Uses pure Java implementation for wall-clock time

  • System.nanoTime(): JVM's monotonic time source

This benchmark measures the overhead (in nanoseconds) of acquiring time from each source.

Test Program

Class: com.neeve.perf.time.Benchmark

The benchmark:

  1. Warms up with 10,000 time acquisitions

  2. Measures 100,000,000 time acquisitions

  3. Reports average overhead per call in nanoseconds

  4. Supports concurrent execution across multiple threads

Time Sources

Native Time

Uses JNI to call native high-resolution clock:

Characteristics:

  • Highest resolution (typically nanosecond precision)

  • Slightly higher overhead due to JNI call

  • Wall-clock time (can go backwards on NTP adjustments)

Use When: You need highest resolution time and can afford JNI overhead

Epoch Time

Uses pure Java implementation:

Characteristics:

  • Pure Java (no JNI overhead)

  • Lower overhead than native

  • Wall-clock time (can go backwards on NTP adjustments)

  • Resolution depends on JVM (typically microsecond precision)

Use When: You need wall-clock time with lower overhead

System.nanoTime()

Uses JVM's monotonic time source:

Characteristics:

  • Lowest overhead

  • Monotonic (never goes backwards)

  • NOT wall-clock time (arbitrary epoch)

  • High resolution

Use When: You need to measure elapsed time or durations

Command-Line Parameters

Parameter
Short
Default
Description

--mode

-m

native

Time source: native, epoch, or nano

--threads

-t

1

Number of concurrent threads

--help

-h

-

Print usage information

Running the Benchmark

Basic Usage

Test Native Time

Example Output:

Test Epoch Time

Example Output:

Test System.nanoTime()

Example Output:

Test with Multiple Threads

Example Output:

Interpreting Results

The benchmark reports the average overhead per time acquisition in nanoseconds.

Typical Results (Linux x86-64)

Time Source
Single Thread
Multiple Threads
Resolution

System.nanoTime()

~20-25ns

~20-25ns

Nanosecond

Epoch Time

~25-30ns

~25-30ns

Microsecond

Native Time

~30-40ns

~30-40ns

Nanosecond

Performance Considerations

  1. Absolute Overhead:

    • All time sources have sub-40ns overhead

    • Overhead is negligible for most applications

    • Matters most in tight loops or very low latency paths

  2. Contention:

    • Time acquisition overhead typically doesn't increase with thread count

    • Modern CPUs have per-core time sources

  3. Use Case Selection:

    • Latency Tracking: Use native or epoch time for wall-clock timestamps

    • Duration Measurement: Use System.nanoTime() for elapsed time

    • Hot Paths: Use epoch time for balance of overhead and resolution

Best Practices

For Latency Measurement

Use wall-clock time (native or epoch) to timestamp events:

For Duration Measurement

Use System.nanoTime() for elapsed time:

For High-Frequency Calls

If time acquisition is in a critical path called millions of times per second, prefer epoch time:

System Properties

Property
Values
Description

nv.time.usenative

true/false

Enable/disable native time (default: true)

Next Steps

Last updated