Measuring execution time

Timing a piece of code: timestamps before and after, repetition, measurement noise, measurements for several sizes, reading the order of growth and the traps that spoil a measurement.

  • 26 cards
  • 6 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. The numbers
  4. The curves
  5. Scenarios
  6. Understand

A few cards from the deck

  • Benchmark: what is it?

    Show the answer

    Running a piece of code under controlled conditions, to measure its duration and compare it with other codes or other data sizes.

    Only the target code should be timed: neither the creation of the data nor the printing of the result.

  • What does the variable duree hold?

    ```python
    from time import perf_counter

    t = [5, 3, 8, 1]
    debut = perf_counter()
    t.sort()
    fin = perf_counter()
    duree = fin - debut
    `

    • The duration of the sort in seconds, as a floating-point number
    • The number of operations the sort ran during the interval
    • The duration of the sort in milliseconds, as a rounded integer
    • The time at which the sort ended, expressed in seconds since midnight
    Show the answer

    The duration of the sort in seconds, as a floating-point number

    perf_counter() has no defined starting point: a single reading means nothing. Their difference is a duration in seconds, with decimals. A number of operations cannot be measured with a clock.

  • To time a piece of code in Python, you read the clock with time.perf_counter() … and … the code, then compute the difference of the two readings.

    Show the answer

    To time a piece of code in Python, you read the clock with time.perf_counter() before and after the code, then compute the difference of the two readings.

    perf_counter() returns the value of a counter in seconds, with a high resolution. Only the difference between two readings means something, not the value of a single reading.

  • Execution time: what is it?

    Show the answer

    The time elapsed between the start and the end of a computation, which depends on the machine, the language and the data.

    Unlike the number of operations, it changes from one machine to another. It is measured in seconds, for instance with time.perf_counter().