single
Challenge: 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.
スワイプしてコーディングを開始
Create a decorator that measures and prints how long a function takes to execute.
- The decorator must use the
timemodule 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.
解答
フィードバックありがとうございます!
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください