How to get timer ticks at a given time in Python?

In this article, we will show you how to get timer ticks at a given time using different functions in Python. Timer ticks represent time measurements that help track execution time, current time, and performance metrics.

The time module in Python provides several functions to work with time measurements. Here are the main approaches to solve this problem ?

Using time.time() Function

The time.time() function returns the current time in seconds since the Unix epoch (January 1, 1970). It's useful for getting timestamps and measuring elapsed time between events.

Example

Here's how to get the current time in seconds ?

import time

# Get current time in seconds
current_time = time.time()
print(f"Current time: {current_time}")

# Measure time difference
start = time.time()
time.sleep(2)  # Sleep for 2 seconds
end = time.time()
print(f"Elapsed time: {end - start} seconds")

The output will be ?

Current time: 1746692443.9878411
Elapsed time: 2.0021234750747681 seconds

Using time.perf_counter() Function

The time.perf_counter() function provides the highest available resolution timer and is ideal for measuring short durations. It returns a monotonic clock value that cannot go backwards.

Example

This example demonstrates measuring execution time of a prime number check ?

import time

# Define a function to check if a number is prime
def is_prime(number):
    if number < 2:
        return False
    for i in range(2, int(number ** 0.5) + 1):
        if number % i == 0:
            return False
    return True

# Measure execution time
number = 17377
start_time = time.perf_counter()

result = is_prime(number)

end_time = time.perf_counter()

print(f"Number {number} is prime: {result}")
print(f"Execution time: {end_time - start_time:.6f} seconds")

The output will be ?

Number 17377 is prime: True
Execution time: 0.001245 seconds

Using time.process_time() Function

The time.process_time() function returns the CPU time consumed by the current process. It excludes time spent sleeping and is useful for measuring actual computation time.

Example

Here's how to measure CPU process time ?

import time

# Get process time at start
start_process_time = time.process_time()

# Perform some computation
total = sum(i * i for i in range(100000))

# Get process time at end
end_process_time = time.process_time()

print(f"Sum of squares: {total}")
print(f"CPU time used: {end_process_time - start_process_time:.6f} seconds")

The output will be ?

Sum of squares: 333328333350000
CPU time used: 0.015625 seconds

Comparison

Function Best For Includes Sleep Time Resolution
time.time() Timestamps, wall clock time Yes System dependent
time.perf_counter() Performance measurements Yes Highest available
time.process_time() CPU usage measurement No High

Conclusion

Use time.perf_counter() for precise performance measurements, time.time() for timestamps and general timing, and time.process_time() to measure actual CPU computation time. Each function serves different timing needs in Python applications.

Updated on: 2026-03-24T19:09:17+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements