So we're going to try a few things in the Python interpreter
to get a sense of how long things take and I've
written in a procedure here that times the execution of a
piece of code. Right, we could just do the timing with
a stopwatch and then we'd have to run really long things
to be able to get a, a reasonably accurate time but
Python has a built in procedure we can use to measure
time, and that's the time clock procedure. So we could try just
using a stop watch 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. So the value of time.clock
will give us the current processor time in seconds. This
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 variables, start and stop,
that's going to give us the amount of time it took
to execute this code. So I've rendered 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 results of the code and the time it
took to run. What's happening here is actually quite exciting.
So we're using Eval. Eval allows us to evaluate
any string as if it were a python expression, so
we're passing in a string here but when we're passing
the string in to eval, it's running it as code.
So we can pass at any python expression. We're going to start
and end the timer and in between, we're going to evaluate the
code and get the result of evaluating that code. So I've
defined this procedure time execution that will give us the time
it takes to evaluate any Python expression. And now we're going to
run some tests and instead of running them through the web
browser IDE like you've been using so far, I'm going to run
them directly in the Python shell, running on my desktop computer, and
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 so 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 wanted to demonstrate how
time execution works. So let's try a
simple example. We're going to time the execution
of the expression 1 plus 1. So that's doing a
simple addition. We're adding two numbers. Well see how long
that takes and when we run time execution, we get
back two results. We get back the result which is
one plus one is two. That's good. And as the
second result, we get the time it took. A little
hard to read so it looks like 8.3 and then
E to the negative 05. So what that means That's
using the scientific notation. The negative 05 is where the decimal point is so
what that is really looks like this. There are four zeros followed
by the 83 dot dot dot and that's a value in the number of seconds.
For the number of seconds it took to run is .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 like the timing's inaccurate. It depends on other things
going on on the machine. We won't get exactly the same
answer. But it's still a very small number. And if 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, and it's a small number of
microseconds. The actual processor time for something that simple is actually
probably lower than that but there are 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.
So this won't tell us much evaluating very simple, very fast evaluations.
物事がどれだけの時間を要するのか理解するために
Pythonインタプリタで試してみましょう
ここにコードの実行時間を計る関数を書きました
ストップウォッチで時間を計ります
正確な時間を得るためには
とても長いものを実行させる必要があります
Pythonにはtimeモジュールがあります
時間を測定するためのtime.clock関数です
ストップウォッチを用意して
十分長い実行時間を要するプログラムを実行すると
どれほど時間がかかるのか把握できます
このtimeモジュールを利用すれば
秒数に評価するtimeモジュールによって
より正確な時間が得られます
time.clockの値は数秒で
現在のプロセッサタイムを出します
この開始点はまったくの任意ですが重要なのは
これを2度呼び出すことです
1度目の呼び出しで開始し 2度目で停止します
これを変数startとstopに保存します
これによりコードを実行するために
費やす時間が分かります
関数はご覧のとおりです
変数startを現在の時刻に設定することから開始し
コードを実行します
何が実行されるかはあとでお話しします
現在の時間とstartの時間との間を計算します
それにより実行時間が分かります
実行にかかった時間とコードの結果の両方を渡します
とてもワクワクすることが起こりますよ
使うのはevalです evalでPython式における
あらゆる文字列を評価することができます
この文字列を渡しますがevalに文字列を
渡したらコードとしてそれを実行します
time_executionには何を渡しても構いません
startから実行が終わるまでの時間を出し
コード実行の結果も出します
このようにtime_execution関数を定義しました
Python式を評価するために要する時間が分かります
これまで使用してきたようなWebブラウザIDを通して
実行する代わりにいくつかテストを実行します
私のデスクトップコンピュータで実行することで
直接Pythonシェルを実行します
より正確な時間測定を行うためです
私たちが提供するWebブラウザインターフェースを
使用しても計測できます
問題はプログラムを実行する時間に制限があり
あまり正確な時間が得られないことです
Pythonシェルでこれらを実行します
Pythonをインストールしてあるなら
自分で試してみてください
使用方法はWebサイトで見ることができますが
クラスで続ける必要が
必ずしもあるわけではないので
time_executionがどのように
機能するのかだけ示します
サンプルを試してみましょう
1 + 1の実行時間を計ります
簡単な2つの数値を追加し
time_executionを実行すると
どれほど時間がかかるのか確認します
2つの結果が得られます 1足す1は2になりますね
2つ目がかかった時間になります
読みにくいですが冒頭は8.3のようですね
そのあとにeー05が続きますがこれは
指数表現を使用するという意味です
ー05は少数点を意味し
小数点後にゼロを4つつけるという意味です
なので実際は0.000083...秒の意味です
実行にかかった時間を秒数にすると
0.00008となります
もう1度同じことを試しても
まったく同じ結果を得られるわけではありません
これは機械内で起きている
他の要因に左右されるからです
まったく同じ解答は得られません
しかしとても小さな数値ですね
あと数回試してもこの小さな数値を
目にし続けることになります
より複雑なものを追加して行った場合
やはりとても小さな数値が得られます
マイクロ秒という小さな数値です
実際のプロセッサタイムそのものは
おそらくこれらよりも
低いものになりますが機械内には
他にも起きているものがありますので
正確な時間を計るためには
他の動作を停止させる必要があります
もっと長いものを試してさらに確認してみましょう
この確認ではより実際に近い評価は分かりません