nanos.time module

class nanos.time.Timer(precision: int = 2)

Bases: object

Initializes a Timer instance with optional precision.

Parameters:

precision (int) – The number of decimal places to use for displaying fractional seconds. Defaults to 2.

precision

Number of decimal places for time display.

Type:

int

start

The start time in seconds since epoch, or None if not started.

Type:

float | None

end

The end time in seconds since epoch, or None if still running.

Type:

float | None

Examples

Basic synchronous usage with context manager:

with Timer() as timer:
    time.sleep(1.5)
print(f"Elapsed: {timer.elapsed}s")
# Output: Elapsed: 1.5s
print(timer.verbose())
# Output: 0:00:01.50

Asynchronous usage with aiohttp:

async with Timer() as timer:
    async with aiohttp.ClientSession() as session:
        response = await session.get('https://api.example.com/data')
        data = await response.json()
print(f"API call took: {timer.elapsed}s")

Asynchronous usage with asyncio:

async with Timer(precision=3) as timer:
    await asyncio.sleep(2.5)
    result = await some_async_function()
print(f"Operation completed in {timer.verbose()}")
# Output: Operation completed in 0:00:02.500

Manual control without context manager:

timer = Timer()
timer.start = time.time()

# Do some work
time.sleep(0.5)

timer.end = time.time()
print(f"Work took: {timer.elapsed}s")

Custom precision for high-resolution timing:

with Timer(precision=4) as timer:
    fast_operation()
print(timer.verbose())
# Output: 0:00:00.0123

Checking elapsed time while timer is still running:

with Timer() as timer:
    time.sleep(1)
    print(f"After 1 second: {timer.elapsed}s")
    time.sleep(1)
    print(f"After 2 seconds: {timer.elapsed}s")
print(f"Final: {timer.elapsed}s")

Using timer in async loops:

total_timer = Timer()
total_timer.start = time.time()

for item in items:
    async with Timer() as request_timer:
        await process_item(item)
    print(f"Item processed in {request_timer.elapsed}s")

total_timer.end = time.time()
print(f"All items processed in {total_timer.verbose()}")

Note

The timer uses time.time() which measures wall-clock time, not CPU time. For CPU time measurement, consider using time.process_time() or similar.

property elapsed: float

Calculates the elapsed time in seconds.

This property computes the difference between the end time and the start time of the Timer. If the Timer has not been started, it returns 0.0. If the Timer is running (i.e., the end time is not set), it uses the current time as the end time.

Returns:

The elapsed time in seconds.

Return type:

float

verbose() str

Returns a formatted string representing the elapsed time with a precision specified by the Timer instance.

The elapsed time is formatted as a string in the format of ‘H:MM:SS.F’, where ‘H:MM:SS’ is the hours, minutes, and seconds, and ‘F’ is the fractional seconds with a number of decimal places equal to the precision.

Returns:

The formatted elapsed time as a string.

Return type:

str