Mistake on this page?
Report an issue in GitHub or email us


Timer class hierarchy

Use the Timer interface to create, start, stop and read a timer for measuring precise times (better than millisecond precision).

You can independently create, start and stop any number of Timer objects.

Warnings and notes

  • Timers are based on 64-bit unsigned microsecond counters, but for backward compatibility, the read_ms() and read_us() methods only return 32-bit signed integers. This limits their range before wrapping to 49 days and 35 minutes respectively. Use read_high_resolution_us() to access the full range of over 500,000 years.
  • While a Timer is running, deep sleep is blocked to maintain accurate timing. If you don't need microsecond precision, consider using the LowPowerTimer class instead because this does not block deep sleep mode.

Timer class reference

Timer hello, world

/* mbed Example Program
 * Copyright (c) 2006-2014 ARM Limited
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
#include "mbed.h"
Timer t;
int main() {
    printf("Hello World!\n");
    printf("The time taken was %f seconds\n", t.read());

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.