{"id":3932,"library":"codetiming","title":"Codetiming","description":"Codetiming is a flexible and customizable Python library for timing code execution. It allows users to measure the performance of different code blocks, functions, or entire scripts using a simple `Timer` class, context manager, or decorator. The current version is 1.4.0, and releases are made on an as-needed basis.","status":"active","version":"1.4.0","language":"en","source_language":"en","source_url":"https://github.com/realpython/codetiming","tags":["timing","performance","profiling","decorator","context manager"],"install":[{"cmd":"pip install codetiming","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"While tutorials might show `from timer import Timer`, the installed package is `codetiming`, requiring import from `codetiming`.","wrong":"from timer import Timer","symbol":"Timer","correct":"from codetiming import Timer"}],"quickstart":{"code":"from codetiming import Timer\nimport time\n\n# As a class\nt = Timer(name=\"class method\")\nt.start()\ntime.sleep(0.01)\nt.stop()\n\n# As a context manager\nwith Timer(name=\"context manager\"):\n    time.sleep(0.02)\n\n# As a decorator\n@Timer(name=\"decorator\")\ndef my_function():\n    time.sleep(0.03)\n\nmy_function()\n\n# Accessing accumulated times for named timers\nprint(f\"Total time for 'class method': {Timer.timers.total('class method'):.4f} seconds\")\nprint(f\"All named timers: {Timer.timers.items()}\")","lang":"python","description":"Demonstrates `codetiming.Timer` used as a class, a context manager, and a decorator. It also shows how to access accumulated statistics for named timers."},"warnings":[{"fix":"Upgrade to Python 3.7+ or pin `codetiming<1.4.0` in your dependencies.","message":"Explicit support for Python 3.6 was dropped in version 1.4.0. Users on Python 3.6 should pin their `codetiming` dependency to `<1.4.0`.","severity":"breaking","affected_versions":">=1.4.0"},{"fix":"For `async` functions, prefer using `Timer` as a class or context manager explicitly measuring `await` calls, or check the GitHub issues for updates on fix.","message":"When using `Timer` as a decorator on `async` functions, the reported time might be 0 or incorrect. This is a known open issue.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use double curly braces for `codetiming`'s placeholders if mixing with f-string formatting, e.g., `Timer(text=f'Task {task_name} finished in {{:.4f}} seconds')`.","message":"The `text` argument for `Timer` is not an f-string. If you want to include f-string-like formatting (e.g., a variable from the current scope) within the `text` string, you must use double curly braces to escape the parts that `codetiming` will format.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware of the accumulation behavior of `Timer.timers`. If single measurements are needed for a named timer, create a new `Timer` instance or delete the existing named timer from `Timer.timers` before reuse. Example: `del Timer.timers['my_timer_name']`.","message":"Named timers accumulate elapsed time across multiple uses. If you reuse a `Timer` instance or a name for `Timer` and expect a fresh measurement, you need to manually reset or manage these timings (e.g., retrieve statistics and then delete the timer from `Timer.timers`).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}