English subtitles

← Stopwatch - Intro to Computer Science

Get Embed Code
2 Languages

Showing Revision 6 created 07/06/2017 by Dejan Trajkovic.

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