1
00:00:00,600 --> 00:00:01,589
Instructor: In this exercise,

2
00:00:01,589 --> 00:00:05,100
you're going to create your very own decorator function

3
00:00:05,100 --> 00:00:07,890
and your decorator function is going to do something

4
00:00:07,890 --> 00:00:09,390
that a lot of you are curious about,

5
00:00:09,390 --> 00:00:11,370
which is how long does it take

6
00:00:11,370 --> 00:00:13,260
for a particular function to run?

7
00:00:13,260 --> 00:00:15,720
Is my function efficient?

8
00:00:15,720 --> 00:00:18,270
Is it as fast as possible?

9
00:00:18,270 --> 00:00:20,640
Well, you'll actually get a numerical answer

10
00:00:20,640 --> 00:00:24,060
to that question by completing this current challenge.

11
00:00:24,060 --> 00:00:26,130
So how is time calculated?

12
00:00:26,130 --> 00:00:29,190
Well, in Python, there is a module called time

13
00:00:29,190 --> 00:00:31,650
and you can run time.time

14
00:00:31,650 --> 00:00:33,900
in order to get the current time

15
00:00:33,900 --> 00:00:38,900
since 1st of January, 1970 at midnight.

16
00:00:39,300 --> 00:00:43,170
Try running the starting code and seeing what gets printed

17
00:00:43,170 --> 00:00:45,930
as the current time each time you run it.

18
00:00:45,930 --> 00:00:46,920
Hopefully you'll notice

19
00:00:46,920 --> 00:00:49,320
that that number increments each time

20
00:00:49,320 --> 00:00:53,400
and it represents the number of seconds since 1970.

21
00:00:53,400 --> 00:00:57,150
So as time goes on, that number will go up.

22
00:00:57,150 --> 00:00:58,920
Think about how you can use that

23
00:00:58,920 --> 00:01:01,470
to create a decorator function

24
00:01:01,470 --> 00:01:03,900
and write your decorator function

25
00:01:03,900 --> 00:01:07,830
inside a speed_calc_decorator, and then apply that

26
00:01:07,830 --> 00:01:11,040
to both the fast_function and the slow_function

27
00:01:11,040 --> 00:01:14,580
in order to see how long it takes each of them to execute.

28
00:01:14,580 --> 00:01:17,610
That's the goal of this challenge, and remember

29
00:01:17,610 --> 00:01:19,920
to take a look inside the description box to see

30
00:01:19,920 --> 00:01:24,360
how it should behave, and also look at the hint

31
00:01:24,360 --> 00:01:26,010
if you get stuck.

32
00:01:26,010 --> 00:01:26,843
Have a go now.

33
00:01:35,084 --> 00:01:36,240
The first thing we need to do

34
00:01:36,240 --> 00:01:39,483
is to define our speed_calc_decorator,

35
00:01:41,220 --> 00:01:43,530
and we're going to add an input

36
00:01:43,530 --> 00:01:46,560
to this function, which is also a function.

37
00:01:46,560 --> 00:01:49,440
It's basically the function that we're gonna be decorating

38
00:01:49,440 --> 00:01:52,950
and now we can access that function inside the body

39
00:01:52,950 --> 00:01:55,740
of the speed_calc_decorator.

40
00:01:55,740 --> 00:01:58,680
Next, we're going to define our wrapper_function.

41
00:01:58,680 --> 00:02:01,740
And this wrapper_function is going to be the one

42
00:02:01,740 --> 00:02:03,783
that's calculating the time.

43
00:02:05,220 --> 00:02:06,300
Inside the wrapper_function,

44
00:02:06,300 --> 00:02:09,090
the first thing we define is the start_time

45
00:02:09,090 --> 00:02:10,800
using time.time.

46
00:02:10,800 --> 00:02:12,720
And this will give us the number of seconds

47
00:02:12,720 --> 00:02:15,510
since January 1st, 1970.

48
00:02:15,510 --> 00:02:18,540
And then we run the function that's being passed in,

49
00:02:18,540 --> 00:02:21,210
which is going to be the function that we decorate.

50
00:02:21,210 --> 00:02:25,230
And this could be the slow_function or the fast_function

51
00:02:25,230 --> 00:02:27,870
or any other function for that matter.

52
00:02:27,870 --> 00:02:30,480
And then after that function is completed,

53
00:02:30,480 --> 00:02:32,460
then we calculate the end_time.

54
00:02:32,460 --> 00:02:36,363
Well, what is the time now since January 1st, 1970?

55
00:02:37,800 --> 00:02:40,230
Finally, we can write our print function

56
00:02:40,230 --> 00:02:44,280
and using an f string, we substitute the function.__name,

57
00:02:44,280 --> 00:02:47,430
which is the name of the function that got passed in,

58
00:02:47,430 --> 00:02:49,620
which is the function that we're decorating.

59
00:02:49,620 --> 00:02:53,520
And then we are printing the run speed of that function.

60
00:02:53,520 --> 00:02:55,320
And that is, of course, calculated

61
00:02:55,320 --> 00:02:58,199
by the end_time subtracting from the start_time.

62
00:02:58,199 --> 00:03:01,560
And we end up with the number of seconds that has elapsed

63
00:03:01,560 --> 00:03:05,493
for the function that was passed in to finish running.

64
00:03:06,360 --> 00:03:09,690
And then finally, we return our wrapper_function

65
00:03:09,690 --> 00:03:13,590
and that completes the definition of our decorator.

66
00:03:13,590 --> 00:03:17,040
Now all that's left to do is to decorate our functions

67
00:03:17,040 --> 00:03:19,050
that we want to apply this to.

68
00:03:19,050 --> 00:03:23,010
So on line 13 and 18, I'm adding the speed_calc_decorator

69
00:03:23,010 --> 00:03:25,620
to the fast_function and the slow_function.

70
00:03:25,620 --> 00:03:27,720
And finally, on line 23 and 24,

71
00:03:27,720 --> 00:03:29,670
I'm actually running those functions,

72
00:03:29,670 --> 00:03:32,973
which will carry along with it the decorator functions.

73
00:03:34,020 --> 00:03:35,670
So now if you ran the code,

74
00:03:35,670 --> 00:03:38,130
you would see how long it took for the fast_function to run,

75
00:03:38,130 --> 00:03:40,380
how long it took for the slow_function to run.

76
00:03:40,380 --> 00:03:41,700
And you can see each time,

77
00:03:41,700 --> 00:03:44,220
we're applying the speed_calc_decorator to each one

78
00:03:44,220 --> 00:03:46,503
of them and printing out the running time.

