Code Explanation:
Importing Required Modules
from functools import reduce
import timeit
reduce is imported from functools — it applies a function cumulatively to a list.
timeit is used to measure execution time.
Statement to Time
stmt = 'reduce(lambda x, y: x * y, [1, 2, 3, 4])'
This is a string of code that:
Uses reduce to multiply all elements of the list [1, 2, 3, 4]
That is:
(((1 * 2) * 3) * 4) = 24
Since this is a string, timeit will treat it as code to execute.
3. Calling timeit
print(timeit.timeit(stmt, number=1000))
This runs the stmt 1,000 times and prints the total time taken in seconds.
Final Output:
D: Error
.png)

0 Comments:
Post a Comment