We're going to try a few things in the Python interpreter to get a sense of how long things take.
I've written a procedure here that times the execution of a piece of code.
We could just do the timing with a stopwatch.
Then we'd have to run really long things to be able to get a reasonably accurate time.
But Python has a built-in procedure we can use to measure time.
That's the time.clock procedure.
We could try just using a stopwatch, and if we ran programs that took long enough to run,
this would give us a reasonable idea of how long they took.
It's going to be a lot more accurate to use this built-in procedure,
which is provided by the time library that evaluates to the number of seconds.
The value of time.clock will give us the current processor time in seconds.
The starting point is fairly arbitrary there,
but the important thing is if we call it twice and we start the time here
and we stop the time at the second call--we store those in the variable start and stop--
that's going to give us the amount of time it took to execute this code.
I've written a procedure that starts by initializing the variable start to the current clock time,
then run some code, and I'll talk more about what it's doing there in a second,
and then it computes the time between the current time and the start time
and gives us the run time, and it returns both the result of the code and the time it took to run.
What's happening here is actually quite exciting.
We're using eval. Eval allows us to evaluate any string as though it were a Python expression.
We're passing in a string here, but when we pass that string into eval, it's running it as code.
We can pass in any Python expression. We're going to start and end the timer.
In between we're going to evaluate the code and get the result of evaluating that code.
I've defined this procedure "time execution" that will give us
the time it takes to evaluate any Python expression.
Now we're going to run some tests, and instead of running them through the web browser ID,
like you've been using so far, I'm going to run them directly in the Python shell
running on my desktop computer.
The reason for that is we'll get more accurate timing measurements.
You can try running timing through the web browser interface that we provide.
The problem is there are limits to how long we let your programs execute,
and the timings won't be very accurate.
Instead, I'm going to run these in the Python shell.
If you have Python installed you can try this yourself,
and we have some directions on the website how you can do that.
But it's not necessary to do that to continue in the class.
I just want to demonstrate how time execution works.
Let's try a simple example.
We're going to time the execution of the expression 1 + 1.
That's doing a simple addition. We're adding two numbers. We'll see how long that takes.
When we run time execution, we get back two results.
We get back the result, which is 1 + 1 is 2--that's good.
As the second result we get the time it took--a little hard to read.
It looks like 8.3 and then e^(-05).
What that means is that is using scientific notation.
The -05 is where the decimal point is, so what that is really looks like this: [0.000083].
There are 4 zeros followed by the 8-3-dot-dot-dot.
That's a value in the number of seconds.
The number of seconds it took to run is 0.00008 seconds.
If we try the same thing again--we'll try timing the exact same thing--
we don't get exactly the same result. The timing is inaccurate.
It depends on other things going on in the machine. We won't get exactly the same answer.
But it's still a very small number, and it we try it a few times, we'll keep seeing this small number.
If we try a more complicated addition, it's still going to give us a very small number.
It's a small number of microseconds.
That actual processor time for something that simple is actually probably lower than that,
but there's some other things happening, because we needed to stop and start the clock
and other things to do the timing.
We'll see more if we try running longer things.
This won't tell us much--evaluating very simple, very fast evaluations.
We're going to define a procedure.