Question

ImportError: can't import name perf_counter. What may be the reason?

I`ve been working on my code for Micropython-flashed ESP8266, and well... this error occured:

from time import perf_counter

ImportError: can't import name perf_counter

I believe that the whole code itself is irrelevant, as the problem occurs during the import itself. What may be relevant though, is the list of functions imported, which looks strange to me:

>>> import time
>>> dir(time)
['__class__', '__name__', '__dict__', 'gmtime', 'localtime', 'mktime', 'sleep', 'sleep_ms', >'sleep_us', 'ticks_add', 'ticks_cpu', 'ticks_diff', 'ticks_ms', 'ticks_us', 'time', 'time_ns']

I'm currently using Python version 3.9.0

I have tried looking up the failure on google, but found only the older failures that are associated with time.clock removal and its later replacement with time.perf_counter.

 3  38  3
1 Jan 1970

Solution

 3

In micropython, according to its documentation the time module doesn't have perf_counter.

2024-07-11
Copperfield

Solution

 1

Thanks everybody for your express-help! And the question can now be marked as answered (did it). For anyone reading it in the future - there is a method already build in Micropython for measuring small time steps, which is:

import time

# Record the start time
start_time = time.ticks_us()

# Function to return elapsed time with precision up to 0.0001 seconds
def elapsed_time():
    current_time = time.ticks_us()
    elapsed = time.ticks_diff(current_time, start_time) / 1_000_000  # Convert microseconds to seconds
    return round(elapsed, 4)
2024-07-11
Alex Kolbeck