This quiz will see if you understand execution time
well enough to make some predictions. So, here's the
code we had before that times the execution of
evaluating a Python expression that we pass in as
code. And we've defined spin loop as a y
loop that goes through a loop that just adds
one the number of times of the variable passed
in. So let's try that in the Python interpreter.
We're going to time an execution where
what we're evaluating is calling spin loop, passing
in some numbers. So we'll try 1,000 first.
And what time execution does is return two
values. The result and the run time. We only want the second one, so we'll index
to get the second value out of the return result, and see that result. So that's
what we get, so it took 0.0001 seconds to do spin loop a thousand. If I increase
this to 10,000, I see that it takes 0.0006. Lets try 100,000,
adding one more zero and I see now it takes 0.005. I'm going to write this a
little differently, so we can see it more easily. So now, instead of writing
out a 100,000, I'll do ten star, star, five, which is the same value as writing
out a one followed by five zeros. And for good measure, we'll do ten star, star,
six, which is a million times through the loop, and we see that, that takes 0.05
seconds. So here's the examples of timing loops.
For your quiz, your goal is to estimate
the expected execution time for a value running spin loop where the input of the
loop is ten star star nine, that's one
billion. And you should give your answer in
the number of seconds. Of course it's not
possible to get the exact answer, but you
should be able to get a guess within about 20% of our answer to be correct.
この小テストでは予測が行えるほど
実行時間を理解しているかどうか確認します
以前のコードがあります これはコードとして渡した
Python式を評価する実行時間を計算するものです
spin_loopの定義があります
whileループもあります
渡した変数に1を加えていくものです
Pythonインタプリタで試してみましょう
いくつかの数値を渡してspin_loopを呼び出し
評価することで実行時間を計算します
まず1,000回試してみましょう
time_executionは2つの値を返します
結果と実行時間ですがここでは2つ目だけが必要です
インデックスし2つ目の値の結果を返します
これが結果です 1,000回ループを行うのに
かかる時間は0.0001秒です
10,000回まで増やすと
0.0006秒かかることが分かります
さらにゼロを追加し100,000回の場合は
0.005秒かかるのが分かります
これを少し変えて書いてみると確認しやすくなります
100,000を書き出す代わりに
10 ** 5と記入します
これは1のあとにゼロを5つ書くことと
同じ意味になります 測定を正確にするために
10 ** 6を行ってみましょう
100万回という意味です 0.05秒かかりました
以上がループの実行時間の例です
ここで小テストです
ループが 10 ** 9つまり10億の場合
spin_loopを実行する時間を
予想してみてください
秒数で答えを出してください
もちろん完全に正確な 答えは得られませんが
答えの誤差が20%以内なら正解とみなします