Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Timing Decorator | Built-in and Real-World Decorators
Quizzes & Challenges
Quizzes
Challenges
/
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.

Uppgift

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.

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 4
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

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

Svep för att visa menyn

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.

Uppgift

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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 4
single

single

some-alt