Skip to content

Unexpected CPU cycle count with time.tick_cpu() #179

@microbit-carlos

Description

@microbit-carlos

With a CPU clock of 64 MHz I would expect time.tick_cpu() to overflow after 17 seconds (2^30 / 64_000_000 = 16.77 sec), and for sleep(1000) to take roughly 64 M cycles, however this script yields differnet results:

from microbit import *
import time


def one_sec_cycles():
    ticks_start = time.ticks_cpu()
    sleep(1000)
    ticks_end = time.ticks_cpu()
    return ticks_end - ticks_start


def time_to_overflow():
    # If this is the first call it will start the counter, so give it some headstart
    ticks_start = time.ticks_cpu()
    sleep(20)
    ticks_start = time.ticks_cpu()
    # Wait until it overflows
    while ticks_start < time.ticks_cpu():
        sleep(1)

    time_start = time.ticks_ms()
    ticks_start = time.ticks_cpu()
    while ticks_start < time.ticks_cpu():
        sleep(1)
    time_end = time.ticks_ms()
    return time_end- time_start


while True:
    print("1 sec: {} cycles".format(one_sec_cycles()))
    print("time to overflow: {} ms".format(time_to_overflow()))

Output:

1 sec: 10834428 cycles
time to overflow: 83293 ms

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions