Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Challenge: Timing Decorator | Built-in and Real-World Decorators
Python Decorators Explained

bookChallenge: Timing Decorator

Measuring how long a function takes to execute is a common requirement when you want to optimize your code or simply understand its performance. In Python, the time module provides functions for working with time-related tasks. When creating a timing decorator, you can use time.time() to record the current time before the function runs and again after it finishes. By subtracting the start time from the end time, you obtain the elapsed time, which you can then print or log. This technique is especially useful for benchmarking or profiling your code without modifying the original function logic.

Taak

Swipe to start coding

Create a decorator that measures and prints how long a function takes to execute.

  • The decorator must use the time module to record the start and end time.
  • The decorator must print the function name and the elapsed time in seconds, formatted to six decimal places.
  • The decorator must return the result of the original function.

Oplossing

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 4
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you show me an example of a timing decorator in Python?

How can I use this timing technique with multiple functions?

Are there any alternatives to using the time module for measuring execution time?

close

bookChallenge: Timing Decorator

Veeg om het menu te tonen

Measuring how long a function takes to execute is a common requirement when you want to optimize your code or simply understand its performance. In Python, the time module provides functions for working with time-related tasks. When creating a timing decorator, you can use time.time() to record the current time before the function runs and again after it finishes. By subtracting the start time from the end time, you obtain the elapsed time, which you can then print or log. This technique is especially useful for benchmarking or profiling your code without modifying the original function logic.

Taak

Swipe to start coding

Create a decorator that measures and prints how long a function takes to execute.

  • The decorator must use the time module to record the start and end time.
  • The decorator must print the function name and the elapsed time in seconds, formatted to six decimal places.
  • The decorator must return the result of the original function.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 4
single

single

some-alt