Python Time time_ns Function

The time_ns function in Python's time module returns the current time in nanoseconds since the Epoch. This function is useful when you need higher precision for time measurements compared to the time function, which provides time in seconds.

Table of Contents

  1. Introduction
  2. time_ns Function Syntax
  3. Examples
    • Basic Usage
    • Measuring Elapsed Time
  4. Real-World Use Case
  5. Conclusion

Introduction

The time_ns function in Python's time module provides the current time in nanoseconds since the Epoch. The Epoch is the point where the time starts, which is platform-dependent but on Unix, it is January 1, 1970, 00:00:00 (UTC). This function is particularly useful for applications requiring high-resolution time measurements.

time_ns Function Syntax

Here is how you use the time_ns function:

import time
current_time_ns = time.time_ns()

Parameters:

  • The time_ns function does not take any parameters.

Returns:

  • An integer representing the current time in nanoseconds since the Epoch.

Examples

Basic Usage

Here is an example of how to use time_ns.

Example

import time

# Getting the current time in nanoseconds
current_time_ns = time.time_ns()
print("Current time in nanoseconds since the Epoch:", current_time_ns)

Output:

Current time in nanoseconds since the Epoch: 1721747072994591700

Measuring Elapsed Time

This example shows how to measure the elapsed time of a code block using time_ns.

Example

import time

# Starting the timer
start_time_ns = time.time_ns()

# Code block whose execution time is to be measured
for i in range(1000000):
    pass

# Stopping the timer
end_time_ns = time.time_ns()

# Calculating the elapsed time
elapsed_time_ns = end_time_ns - start_time_ns
print("Elapsed time:", elapsed_time_ns, "nanoseconds")

Output:

Elapsed time: 18966000 nanoseconds

Real-World Use Case

High-Resolution Timing for Performance Testing

In real-world applications, the time_ns function can be used to perform high-resolution timing for performance testing, ensuring that even very short durations are measured accurately.

Example

import time

def high_precision_task():
    sum = 0
    for i in range(1000000):
        sum += i
    return sum

# Measuring the high precision task execution time
start_time_ns = time.time_ns()
result = high_precision_task()
end_time_ns = time.time_ns()

# Calculating the elapsed time in nanoseconds
elapsed_time_ns = end_time_ns - start_time_ns
print(f"Task result: {result}")
print(f"Elapsed time: {elapsed_time_ns} nanoseconds")

Output:

Task result: 499999500000
Elapsed time: 25589100 nanoseconds

Conclusion

The time_ns function provides the current time in nanoseconds since the Epoch, offering higher precision than the time function. This makes it useful for high-resolution timing and performance measurements.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare