1 00:00:00,530 --> 00:00:02,910 So we're going to try a few things in the Python interpreter 2 00:00:02,910 --> 00:00:05,790 to get a sense of how long things take and I've 3 00:00:05,790 --> 00:00:09,730 written in a procedure here that times the execution of a 4 00:00:09,730 --> 00:00:12,840 piece of code. Right, we could just do the timing with 5 00:00:12,840 --> 00:00:15,800 a stopwatch and then we'd have to run really long things 6 00:00:15,800 --> 00:00:18,420 to be able to get a, a reasonably accurate time but 7 00:00:18,420 --> 00:00:20,810 Python has a built in procedure we can use to measure 8 00:00:20,810 --> 00:00:25,620 time, and that's the time clock procedure. So we could try just 9 00:00:25,620 --> 00:00:27,740 using a stop watch and if we ran programs 10 00:00:27,740 --> 00:00:30,030 that took long enough to run, this would give us 11 00:00:30,030 --> 00:00:32,540 a reasonable idea of how long they took. It's going to 12 00:00:32,540 --> 00:00:34,760 be a lot more accurate to use this built in 13 00:00:34,760 --> 00:00:41,250 procedure, which is provided by the time library that evaluates 14 00:00:41,250 --> 00:00:44,700 to the number of seconds. So the value of time.clock 15 00:00:44,700 --> 00:00:47,390 will give us the current processor time in seconds. This 16 00:00:47,390 --> 00:00:50,740 starting point is fairly arbitrary there but the important thing 17 00:00:50,740 --> 00:00:53,390 is if we call it twice, and we start the 18 00:00:53,390 --> 00:00:57,120 time here, and we stop the time at the second 19 00:00:57,120 --> 00:01:00,460 call, we store those in the variables, start and stop, 20 00:01:00,460 --> 00:01:03,650 that's going to give us the amount of time it took 21 00:01:03,650 --> 00:01:06,670 to execute this code. So I've rendered a procedure that 22 00:01:06,670 --> 00:01:09,630 starts by initializing the variable start to the current clock 23 00:01:09,630 --> 00:01:13,420 time then run some code, and I'll talk more about 24 00:01:13,420 --> 00:01:15,925 what it's doing there in a second, and then it computes 25 00:01:15,925 --> 00:01:18,900 the time between the current time and the start 26 00:01:18,900 --> 00:01:21,430 time, and gives us the run time, and it returns 27 00:01:21,430 --> 00:01:23,880 both the results of the code and the time it 28 00:01:23,880 --> 00:01:27,080 took to run. What's happening here is actually quite exciting. 29 00:01:27,080 --> 00:01:31,520 So we're using Eval. Eval allows us to evaluate 30 00:01:31,520 --> 00:01:35,130 any string as if it were a python expression, so 31 00:01:35,130 --> 00:01:38,350 we're passing in a string here but when we're passing 32 00:01:38,350 --> 00:01:40,460 the string in to eval, it's running it as code. 33 00:01:41,460 --> 00:01:45,160 So we can pass at any python expression. We're going to start 34 00:01:45,160 --> 00:01:47,920 and end the timer and in between, we're going to evaluate the 35 00:01:47,920 --> 00:01:50,791 code and get the result of evaluating that code. So I've 36 00:01:50,791 --> 00:01:54,450 defined this procedure time execution that will give us the time 37 00:01:54,450 --> 00:01:57,650 it takes to evaluate any Python expression. And now we're going to 38 00:01:57,650 --> 00:02:00,080 run some tests and instead of running them through the web 39 00:02:00,080 --> 00:02:02,940 browser ID like you've been using so far, I'm going to run 40 00:02:02,940 --> 00:02:06,980 them directly in the Python shell, running on my desktop computer, and 41 00:02:06,980 --> 00:02:10,110 the reason for that is we'll get more accurate timing measurements. You 42 00:02:10,110 --> 00:02:13,955 can try running timing through the Web browser interface that we provide. 43 00:02:13,955 --> 00:02:16,630 The problem is there are limits to how long we let your 44 00:02:16,630 --> 00:02:20,640 programs execute and the timings won't be very accurate so instead, I'm 45 00:02:20,640 --> 00:02:23,700 going to run these in the Python shell, If you have Python installed, 46 00:02:23,700 --> 00:02:26,410 you can try this yourself, and we have some directions on the 47 00:02:26,410 --> 00:02:29,500 website how you can do that, but it's not necessary to do 48 00:02:29,500 --> 00:02:32,270 that to continue in the class. I just wanted to demonstrate how 49 00:02:32,270 --> 00:02:34,630 time execution works. So let's try a 50 00:02:34,630 --> 00:02:36,800 simple example. We're going to time the execution 51 00:02:38,920 --> 00:02:42,050 of the expression 1 plus 1. So that's doing a 52 00:02:42,050 --> 00:02:45,720 simple addition. We're adding two numbers. Well see how long 53 00:02:45,720 --> 00:02:49,270 that takes and when we run time execution, we get 54 00:02:49,270 --> 00:02:51,960 back two results. We get back the result which is 55 00:02:51,960 --> 00:02:53,830 one plus one is two. That's good. And as the 56 00:02:53,830 --> 00:02:57,760 second result, we get the time it took. A little 57 00:02:57,760 --> 00:03:01,060 hard to read so it looks like 8.3 and then 58 00:03:01,060 --> 00:03:04,860 E to the negative 05. So what that means That's 59 00:03:04,860 --> 00:03:09,450 using the scientific notation. The negative 05 is where the decimal point is so 60 00:03:09,450 --> 00:03:15,170 what that is really looks like this. There are four zeros followed 61 00:03:15,170 --> 00:03:21,200 by the 83 dot dot dot and that's a value in the number of seconds. 62 00:03:21,200 --> 00:03:26,082 For the number of seconds it took to run is .00008 63 00:03:26,082 --> 00:03:29,960 seconds. If we try the same thing again we'll 64 00:03:29,960 --> 00:03:32,880 try timing the exact same thing. We don't get exactly the 65 00:03:32,880 --> 00:03:36,550 same result like the timing's inaccurate. It depends on other things 66 00:03:36,550 --> 00:03:38,510 going on on the machine. We won't get exactly the same 67 00:03:38,510 --> 00:03:40,540 answer. But it's still a very small number. And if we 68 00:03:40,540 --> 00:03:44,230 try it a few times, we'll keep seeing this small number. 69 00:03:44,230 --> 00:03:48,970 If we try, a more complicated addition, it's still going to give 70 00:03:48,970 --> 00:03:51,440 us a very small number, and it's a small number of 71 00:03:51,440 --> 00:03:55,250 microseconds. The actual processor time for something that simple is actually 72 00:03:55,250 --> 00:03:58,750 probably lower than that but there are some other things happening because 73 00:03:58,750 --> 00:04:01,490 we needed to stop and start the clock and other things to 74 00:04:01,490 --> 00:04:05,170 do the timing. We'll see more if we try running longer things. 75 00:04:05,170 --> 00:04:10,110 So this won't tell us much evaluating very simple, very fast evaluations.