Skip to content

Measuring elapsed time [ref]

Problem

  • Record two timestamps with time.time and compute elapsed seconds.
  • Measure how long sum(range(1000)) takes using time.perf_counter.
  • Test whether the second elapsed duration is a float.

Python solution

import time

start = time.time()
end = time.time()
elapsed = end - start
isinstance(elapsed, float)  # True

start = time.perf_counter()
sum(range(1000))
elapsed = time.perf_counter() - start
isinstance(elapsed, float)  # True

isinstance(elapsed, float)  # True