Instructor: In this exercise,
you're going to create your very own decorator function
and your decorator function is going to do something
that a lot of you are curious about,
which is how long does it take
for a particular function to run?
Is my function efficient?
Is it as fast as possible?
Well, you'll actually get a numerical answer
to that question by completing this current challenge.
So how is time calculated?
Well, in Python, there is a module called time
and you can run time.time
in order to get the current time
since 1st of January, 1970 at midnight.
Try running the starting code and seeing what gets printed
as the current time each time you run it.
Hopefully you'll notice
that that number increments each time
and it represents the number of seconds since 1970.
So as time goes on, that number will go up.
Think about how you can use that
to create a decorator function
and write your decorator function
inside a speed_calc_decorator, and then apply that
to both the fast_function and the slow_function
in order to see how long it takes each of them to execute.
That's the goal of this challenge, and remember
to take a look inside the description box to see
how it should behave, and also look at the hint
if you get stuck.
Have a go now.
The first thing we need to do
is to define our speed_calc_decorator,
and we're going to add an input
to this function, which is also a function.
It's basically the function that we're gonna be decorating
and now we can access that function inside the body
of the speed_calc_decorator.
Next, we're going to define our wrapper_function.
And this wrapper_function is going to be the one
that's calculating the time.
Inside the wrapper_function,
the first thing we define is the start_time
using time.time.
And this will give us the number of seconds
since January 1st, 1970.
And then we run the function that's being passed in,
which is going to be the function that we decorate.
And this could be the slow_function or the fast_function
or any other function for that matter.
And then after that function is completed,
then we calculate the end_time.
Well, what is the time now since January 1st, 1970?
Finally, we can write our print function
and using an f string, we substitute the function.__name,
which is the name of the function that got passed in,
which is the function that we're decorating.
And then we are printing the run speed of that function.
And that is, of course, calculated
by the end_time subtracting from the start_time.
And we end up with the number of seconds that has elapsed
for the function that was passed in to finish running.
And then finally, we return our wrapper_function
and that completes the definition of our decorator.
Now all that's left to do is to decorate our functions
that we want to apply this to.
So on line 13 and 18, I'm adding the speed_calc_decorator
to the fast_function and the slow_function.
And finally, on line 23 and 24,
I'm actually running those functions,
which will carry along with it the decorator functions.
So now if you ran the code,
you would see how long it took for the fast_function to run,
how long it took for the slow_function to run.
And you can see each time,
we're applying the speed_calc_decorator to each one
of them and printing out the running time.