function::local_clock_ns - Linux


The function::local_clock_ns command, part of the LLVM function library, provides a way to retrieve the current time in nanoseconds with high accuracy. It is especially useful in high-performance computing scenarios where precise timing is crucial.


void function::local_clock_ns(bool is_monotonic,
                              int64_t* out_seconds,
                              uint32_t* out_nanos);


| Option | Description |
| is_monotonic | If true, the returned time is monotonic and will never go backward. If false, the time may be non-monotonic. |


Get the current time in nanoseconds:

int64_t seconds;
uint32_t nanos;

function::local_clock_ns(false, &seconds, &nanos);

std::cout << "Current time: " << seconds << "." << std::setfill('0') << std::setw(9) << nanos << " seconds" << std::endl;

Get a monotonic time value:

function::local_clock_ns(true, &seconds, &nanos);

// Use the time value for precise timing or measurement

Common Issues

  • Incorrect time values: Ensure that the is_monotonic parameter is set correctly based on the desired behavior. Non-monotonic time values can cause unexpected results in timing-sensitive applications.
  • Portability: The precision and behavior of this function may vary across platforms. Consider using a portable time measurement library such as Boost.Clock if portability is essential.


The function::local_clock_ns command can be integrated with other Linux commands or tools for advanced timing and measurement tasks. For example:

  • Use it in performance profiling scripts to measure the execution time of specific code blocks.
  • Integrate it into custom benchmarking tools to provide accurate timing data.

Related Commands

  • clock_gettime: Retrieve the current time in nanoseconds with less precision.
  • gettimeofday: Retrieve the current time with microsecond precision.