Return to Video

Stopwatch - Intro to Computer Science

  • 0:01 - 0:03
    So we're going to try a few things in the Python interpreter
  • 0:03 - 0:06
    to get a sense of how long things take and I've
  • 0:06 - 0:10
    written in a procedure here that times the execution of a
  • 0:10 - 0:13
    piece of code. Right, we could just do the timing with
  • 0:13 - 0:16
    a stopwatch and then we'd have to run really long things
  • 0:16 - 0:18
    to be able to get a, a reasonably accurate time but
  • 0:18 - 0:21
    Python has a built in procedure we can use to measure
  • 0:21 - 0:26
    time, and that's the time clock procedure. So we could try just
  • 0:26 - 0:28
    using a stop watch and if we ran programs
  • 0:28 - 0:30
    that took long enough to run, this would give us
  • 0:30 - 0:33
    a reasonable idea of how long they took. It's going to
  • 0:33 - 0:35
    be a lot more accurate to use this built in
  • 0:35 - 0:41
    procedure, which is provided by the time library that evaluates
  • 0:41 - 0:45
    to the number of seconds. So the value of time.clock
  • 0:45 - 0:47
    will give us the current processor time in seconds. This
  • 0:47 - 0:51
    starting point is fairly arbitrary there but the important thing
  • 0:51 - 0:53
    is if we call it twice, and we start the
  • 0:53 - 0:57
    time here, and we stop the time at the second
  • 0:57 - 1:00
    call, we store those in the variables, start and stop,
  • 1:00 - 1:04
    that's going to give us the amount of time it took
  • 1:04 - 1:07
    to execute this code. So I've rendered a procedure that
  • 1:07 - 1:10
    starts by initializing the variable start to the current clock
  • 1:10 - 1:13
    time then run some code, and I'll talk more about
  • 1:13 - 1:16
    what it's doing there in a second, and then it computes
  • 1:16 - 1:19
    the time between the current time and the start
  • 1:19 - 1:21
    time, and gives us the run time, and it returns
  • 1:21 - 1:24
    both the results of the code and the time it
  • 1:24 - 1:27
    took to run. What's happening here is actually quite exciting.
  • 1:27 - 1:32
    So we're using Eval. Eval allows us to evaluate
  • 1:32 - 1:35
    any string as if it were a python expression, so
  • 1:35 - 1:38
    we're passing in a string here but when we're passing
  • 1:38 - 1:40
    the string in to eval, it's running it as code.
  • 1:41 - 1:45
    So we can pass at any python expression. We're going to start
  • 1:45 - 1:48
    and end the timer and in between, we're going to evaluate the
  • 1:48 - 1:51
    code and get the result of evaluating that code. So I've
  • 1:51 - 1:54
    defined this procedure time execution that will give us the time
  • 1:54 - 1:58
    it takes to evaluate any Python expression. And now we're going to
  • 1:58 - 2:00
    run some tests and instead of running them through the web
  • 2:00 - 2:03
    browser ID like you've been using so far, I'm going to run
  • 2:03 - 2:07
    them directly in the Python shell, running on my desktop computer, and
  • 2:07 - 2:10
    the reason for that is we'll get more accurate timing measurements. You
  • 2:10 - 2:14
    can try running timing through the Web browser interface that we provide.
  • 2:14 - 2:17
    The problem is there are limits to how long we let your
  • 2:17 - 2:21
    programs execute and the timings won't be very accurate so instead, I'm
  • 2:21 - 2:24
    going to run these in the Python shell, If you have Python installed,
  • 2:24 - 2:26
    you can try this yourself, and we have some directions on the
  • 2:26 - 2:30
    website how you can do that, but it's not necessary to do
  • 2:30 - 2:32
    that to continue in the class. I just wanted to demonstrate how
  • 2:32 - 2:35
    time execution works. So let's try a
  • 2:35 - 2:37
    simple example. We're going to time the execution
  • 2:39 - 2:42
    of the expression 1 plus 1. So that's doing a
  • 2:42 - 2:46
    simple addition. We're adding two numbers. Well see how long
  • 2:46 - 2:49
    that takes and when we run time execution, we get
  • 2:49 - 2:52
    back two results. We get back the result which is
  • 2:52 - 2:54
    one plus one is two. That's good. And as the
  • 2:54 - 2:58
    second result, we get the time it took. A little
  • 2:58 - 3:01
    hard to read so it looks like 8.3 and then
  • 3:01 - 3:05
    E to the negative 05. So what that means That's
  • 3:05 - 3:09
    using the scientific notation. The negative 05 is where the decimal point is so
  • 3:09 - 3:15
    what that is really looks like this. There are four zeros followed
  • 3:15 - 3:21
    by the 83 dot dot dot and that's a value in the number of seconds.
  • 3:21 - 3:26
    For the number of seconds it took to run is .00008
  • 3:26 - 3:30
    seconds. If we try the same thing again we'll
  • 3:30 - 3:33
    try timing the exact same thing. We don't get exactly the
  • 3:33 - 3:37
    same result like the timing's inaccurate. It depends on other things
  • 3:37 - 3:39
    going on on the machine. We won't get exactly the same
  • 3:39 - 3:41
    answer. But it's still a very small number. And if we
  • 3:41 - 3:44
    try it a few times, we'll keep seeing this small number.
  • 3:44 - 3:49
    If we try, a more complicated addition, it's still going to give
  • 3:49 - 3:51
    us a very small number, and it's a small number of
  • 3:51 - 3:55
    microseconds. The actual processor time for something that simple is actually
  • 3:55 - 3:59
    probably lower than that but there are some other things happening because
  • 3:59 - 4:01
    we needed to stop and start the clock and other things to
  • 4:01 - 4:05
    do the timing. We'll see more if we try running longer things.
  • 4:05 - 4:10
    So this won't tell us much evaluating very simple, very fast evaluations.
Title:
Stopwatch - Intro to Computer Science
Description:

18-05 Stopwatch

more » « less
Video Language:
English
Team:
Udacity
Project:
CS101 - Intro to Computer Science
Duration:
04:14

English subtitles

Revisions Compare revisions