WEBVTT 1 00:00:00.120 --> 00:00:01.332 line:15% Hi and welcome back. 2 00:00:01.332 --> 00:00:04.300 line:15% As well the date time module used to deal with 3 00:00:04.300 --> 00:00:06.330 objects containing both date and time, 4 00:00:06.330 --> 00:00:10.410 we have a date module, and a separate time module. 5 00:00:10.410 --> 00:00:13.930 The dates module is used to deal with dates, 6 00:00:13.930 --> 00:00:17.490 like day of the month, and year, and so forth, not time. 7 00:00:17.490 --> 00:00:20.260 And the time module is used to deal just with times. 8 00:00:21.310 --> 00:00:22.779 So whenever you're running some code, 9 00:00:22.779 --> 00:00:26.620 you can measure the start time and the end time to calculate 10 00:00:26.620 --> 00:00:29.625 the total amount of time it took for the code to run. 11 00:00:29.625 --> 00:00:32.129 That's very useful, for example, when you're wondering 12 00:00:32.129 --> 00:00:35.820 whether doing, whether running one function or another 13 00:00:35.820 --> 00:00:38.130 is going to be more efficient. 14 00:00:38.130 --> 00:00:39.290 So, it's really straightforward. 15 00:00:39.290 --> 00:00:42.070 All you have to do is import time. 16 00:00:42.070 --> 00:00:44.380 Oh, time, there. 17 00:00:45.340 --> 00:00:46.760 Then you can define, for example, 18 00:00:46.760 --> 00:00:48.760 the function you want to time. 19 00:00:49.730 --> 00:00:51.480 Let's define this power function, and then we're gonna 20 00:00:51.480 --> 00:00:55.470 return x to the power of two for x in range limit. 21 00:00:56.843 --> 00:01:00.300 This is gonna generate a list of powers, 22 00:01:00.300 --> 00:01:03.950 and up until the limit that we pass in as a parameter. 23 00:01:03.950 --> 00:01:06.620 We're gonna then say start is time.time. 24 00:01:06.620 --> 00:01:08.814 Again, we're initialising an object of type time, 25 00:01:08.814 --> 00:01:11.580 which lives inside the time module. 26 00:01:12.890 --> 00:01:16.130 And end is going to be time.time, and in between these two, 27 00:01:16.130 --> 00:01:17.310 between the start and the end, 28 00:01:17.310 --> 00:01:19.730 we can put in what we want to time. 29 00:01:22.990 --> 00:01:25.580 Let's say power's 50,000. 30 00:01:26.770 --> 00:01:28.470 So we start a timer. 31 00:01:28.470 --> 00:01:30.380 That tells, well, we don't start a timer. 32 00:01:30.380 --> 00:01:31.213 Sorry about that. 33 00:01:31.213 --> 00:01:33.690 We get the current time, and we 34 00:01:33.690 --> 00:01:35.530 store that in the start variable. 35 00:01:36.410 --> 00:01:38.909 Then we run our function, and the end of the function, 36 00:01:38.909 --> 00:01:40.530 we get the current time, and we 37 00:01:40.530 --> 00:01:42.610 store it on our end variable. 38 00:01:42.610 --> 00:01:44.486 Now we've got the starting time and the ending time. 39 00:01:44.486 --> 00:01:49.486 We can print end - start, and see how long it took. 40 00:01:49.530 --> 00:01:50.960 This should be power. 41 00:01:52.250 --> 00:01:53.083 Let's run it. 42 00:01:54.120 --> 00:01:57.590 As you can see, it took 0.066 seconds 43 00:01:57.590 --> 00:02:00.700 to run this function here. 44 00:02:00.700 --> 00:02:02.940 Notice that is not to run the entire programme, 45 00:02:02.940 --> 00:02:04.720 since we'd only started the timer here 46 00:02:04.720 --> 00:02:07.200 and ended it here, so this time doesn't 47 00:02:07.200 --> 00:02:09.640 include defining the function, dealing with imports, 48 00:02:09.640 --> 00:02:11.932 starting up Python, printing the stuff. 49 00:02:11.932 --> 00:02:14.090 Only the function itself. 50 00:02:16.151 --> 00:02:19.410 Pretty useful when you want to check how long a function 51 00:02:19.410 --> 00:02:22.290 has taken, and just in case you think something's going 52 00:02:22.290 --> 00:02:24.770 very slowly in your programmes, or whether you're wondering 53 00:02:24.770 --> 00:02:27.460 which one is more the efficient, one or the other. 54 00:02:27.460 --> 00:02:29.190 Pretty useful thing to do. 55 00:02:29.190 --> 00:02:32.570 Of course you can create a function, 56 00:02:32.570 --> 00:02:35.230 a higher order function, that will do this timing for you. 57 00:02:35.230 --> 00:02:38.510 Like this measure runtime function. 58 00:02:38.510 --> 00:02:41.270 It's going to take in a function as a parameter. 59 00:02:41.270 --> 00:02:42.850 And what it's going to do is just that. 60 00:02:42.850 --> 00:02:45.060 Time.time at the start. 61 00:02:45.060 --> 00:02:48.700 It's gonna run the function, gonna calculate time.time 62 00:02:48.700 --> 00:02:51.530 at the end, and it's going to print end - start. 63 00:02:53.430 --> 00:02:54.263 Okay. 64 00:02:55.360 --> 00:02:59.599 So now this function here is going to measure 65 00:02:59.599 --> 00:03:04.599 the runtime of func, which is a first class function. 66 00:03:06.470 --> 00:03:10.260 So we can call it measure runtime, and then we can pass it. 67 00:03:10.260 --> 00:03:13.361 The result of calling the power to a limit, 68 00:03:13.361 --> 00:03:18.361 that's a lambda is power 50,000. 69 00:03:19.350 --> 00:03:23.240 Notice how measure runtime takes in a function, 70 00:03:24.400 --> 00:03:27.510 which would be the power function, so. 71 00:03:28.870 --> 00:03:31.640 But of course we cannot pass it any arguments, 72 00:03:31.640 --> 00:03:34.240 'cause as soon as we do 50,000 here, 73 00:03:34.240 --> 00:03:38.060 this runs the function, and then passes the output 74 00:03:38.060 --> 00:03:40.830 of the function to the func variable. 75 00:03:40.830 --> 00:03:45.280 If we want to pass it a function, we'll have to do 76 00:03:45.280 --> 00:03:49.040 lambda, power 50,000, like so. 77 00:03:50.700 --> 00:03:53.650 And then of course we will get the output printed out 78 00:03:53.650 --> 00:03:55.580 so we can run this, and see that it takes 79 00:03:55.580 --> 00:03:57.340 more or less the same amount of time. 80 00:03:58.750 --> 00:04:01.090 So, this thing here, this lambda function, is actually 81 00:04:01.090 --> 00:04:03.480 a workaround to some other technique that we're 82 00:04:03.480 --> 00:04:05.830 gonna look at very soon, that I'm gonna teach you 83 00:04:05.830 --> 00:04:08.330 when we start looking at decorators. 84 00:04:08.330 --> 00:04:10.930 But for now, measure runtime is a high order function, 85 00:04:10.930 --> 00:04:14.037 func is a first class function, and this is a 86 00:04:14.037 --> 00:04:15.858 nice, nifty thing you can use in order to 87 00:04:15.858 --> 00:04:19.620 measure some runtime of your functions. 88 00:04:19.620 --> 00:04:22.910 If you want to time the execution of a small code snippet, 89 00:04:22.910 --> 00:04:25.340 like this one here, you can also 90 00:04:25.340 --> 00:04:27.870 look into the timeit module. 91 00:04:27.870 --> 00:04:32.830 So we can import timeit, and instead of calling 92 00:04:32.830 --> 00:04:35.710 our function manually, you know, and setting our timers, 93 00:04:35.710 --> 00:04:39.550 and so forth, we can use a timeit module to time something. 94 00:04:39.550 --> 00:04:44.550 So that's timeit.timeit, and say x, 95 00:04:44.593 --> 00:04:47.630 to the power of two for x in range 10. 96 00:04:49.770 --> 00:04:51.270 And what this is going to do is 97 00:04:51.270 --> 00:04:53.650 it's going to run this statement here. 98 00:04:53.650 --> 00:04:55.451 Notice how it's a string, because that's 99 00:04:55.451 --> 00:04:58.275 going straight to the timeit function. 100 00:04:58.275 --> 00:05:00.040 Timeit function's gonna setup everything, 101 00:05:00.040 --> 00:05:03.170 and it's going to run the function here. 102 00:05:03.170 --> 00:05:04.530 And then it's going to print it out. 103 00:05:04.530 --> 00:05:06.670 By the way the timeit function is going to run it, 104 00:05:06.670 --> 00:05:10.460 I think it's at 10,000 times by default. 105 00:05:10.460 --> 00:05:12.540 And it's going to calculate the average. 106 00:05:14.030 --> 00:05:17.590 So there you have it, 0.0183. 107 00:05:17.590 --> 00:05:18.590 That's pretty quick. 108 00:05:19.500 --> 00:05:24.310 You can also, of course, try to time it against a map. 109 00:05:24.310 --> 00:05:26.390 So let's map it, map. 110 00:05:26.390 --> 00:05:30.970 Lambda x x**2, range 10. 111 00:05:32.860 --> 00:05:33.910 And see what happens. 112 00:05:42.320 --> 00:05:44.210 This seems to be going very slowly. 113 00:05:44.210 --> 00:05:47.189 It went very slowly, and I thought it was we messed 114 00:05:47.189 --> 00:05:50.570 something up, but now it took 8.3 seconds 115 00:05:51.560 --> 00:05:54.510 instead of 0.something seconds. 116 00:05:54.510 --> 00:05:56.210 I don't think we changed anything. 117 00:05:58.630 --> 00:06:00.140 Very strange. 118 00:06:02.480 --> 00:06:03.520 Very strange indeed. 119 00:06:03.520 --> 00:06:05.100 Anyway, I don't know. 120 00:06:05.100 --> 00:06:05.933 I don't know what happened there. 121 00:06:05.933 --> 00:06:08.880 It took 8.5 seconds before, 7.2 seconds now. 122 00:06:08.880 --> 00:06:10.120 I think this editor here is maybe 123 00:06:10.120 --> 00:06:12.140 not the fastest thing in the world. 124 00:06:12.140 --> 00:06:14.355 You may be better off trying this in your local computers. 125 00:06:14.355 --> 00:06:17.660 But nonetheless, you can see that the map and the function 126 00:06:17.660 --> 00:06:21.490 actually takes much less time than the list comprehension. 127 00:06:21.490 --> 00:06:24.800 So there's something to say definitely for using map, 128 00:06:24.800 --> 00:06:28.530 and in some cases map will be more effective, 129 00:06:28.530 --> 00:06:30.837 in order cases less comprehension will be more effective. 130 00:06:30.837 --> 00:06:34.160 Either way, it's not a big deal. 131 00:06:34.160 --> 00:06:36.881 I know that this looks like a lot, seven times faster, 132 00:06:36.881 --> 00:06:41.200 but it's running at 10,000 times, but if we're running it 133 00:06:41.200 --> 00:06:44.150 like a few times, it doesn't matter. 134 00:06:44.150 --> 00:06:46.156 And but nonetheless, if you're worried about performance, 135 00:06:46.156 --> 00:06:49.780 feel free to time things out and compare them, 136 00:06:49.780 --> 00:06:52.040 and then use whichever's most efficient. 137 00:06:52.040 --> 00:06:54.380 Make sure they do do the same thing, though. 138 00:06:54.380 --> 00:06:56.200 It's no good if one of them's faster than the other, 139 00:06:56.200 --> 00:06:57.900 but they do different things. 140 00:06:57.900 --> 00:06:59.250 Okay, that's it for this video. 141 00:06:59.250 --> 00:07:00.730 Just wanted to show you real quick 142 00:07:00.730 --> 00:07:03.417 how can use the time and timeit modules 143 00:07:03.417 --> 00:07:06.727 to time your code, and see how fast it's running. 144 00:07:06.727 --> 00:07:08.277 I'll see you on the next video.