Jupyter Notebook で処理時間を測りました

前回、リスト内包表記のパフォーマンスを計測したときに、 Jupyter Notebook のマジックコマンドを調べましたので書いておきます。

環境

  • Python 3.5.2
  • jupyter-1.0.0

%time

%time

Time execution of a Python statement or expression.

The CPU and wall clock times are printed, and the value of the expression (if any) is returned. Note that under Win32, system time is always reported as 0, since it can not be measured.

This function can be used both as a line and cell magic:

  • In line mode you can time a single-line statement (though multiple ones can be chained with using semicolons).

  • In cell mode, you can time the cell body (a directly following statement raises an error).

    This function provides very basic timing functionality. Use the timeit magic for more control over the measurement.

%time

line mode と cell mode があるようです。 1 行の時間を計測する場合は line mode の %time を使うようです。 セルの時間を計測する場合は cell mode の %%time を使うようです。 cell mode はパーセント記号が二つのようです。

line mode

次のような記述をして、

%time i = sum([1 for x in range(0, 1000000)])
i

実行すると、

# CPU times: user 109 ms, sys: 0 ns, total: 109 ms
# Wall time: 114 ms
# 1000000

Elapsed real time, real time, wall-clock time, or wall time is the actual time taken from the start of a computer program to the end. In other words, it is the difference between the time at which a task finishes and the time at which the task started.

Elapsed real time

Wall time は「実世界」の時間のようです。 この場合は 114 ms のようです。

cell mode

次のような記述をして、

%%time
i = 0
for x in range(0, 1000000):
    i += 1
i

実行すると、

# CPU times: user 188 ms, sys: 0 ns, total: 188 ms
# Wall time: 190 ms

cell mode は最後に記述した変数を Out のセルに出力してくれないようでした。

%timeit

%timeit

Time execution of a Python statement or expression

Usage, in line mode:

%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement

or in cell mode:

%%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code code code…

Time execution of a Python statement or expression using the timeit module. This function can be used both as a line and cell magic:

  • In line mode you can time a single-line statement (though multiple ones can be chained with using semicolons).
  • In cell mode, the statement in the first line is used as setup code (executed but not timed) and the body of the cell is timed. The cell body has access to any variables created in the setup code.

Options: -n<N>: execute the given statement <N> times in a loop. If this value is not given, a fitting value is chosen.

-r<R>: repeat the loop iteration <R> times and take the best result. Default: 3

-t: use time.time to measure the time, which is the default on Unix. This function measures wall time.

-c: use time.clock to measure the time, which is the default on Windows and measures wall time. On Unix, resource.getrusage is used instead and returns the CPU user time.

-p<P>: use a precision of <P> digits to display the timing result. Default: 3

-q: Quiet, do not print result.

-o: return a TimeitResult that can be stored in a variable to inspect

the result in more details.

%timeit

%timeit は与えられた処理を繰り返して処理時間を計測してくれるようです。 オプションで繰り返す数を指定できるようです。

%timeit も line mode と cell mode があって、 cell mode はパーセント記号が二つのようです。

line mode

次のような記述をして、

%timeit i = sum([1 for x in range(0, 1000000)])
i

実行すると、

# 74.8 ms ± 692 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
# NameError: name 'i' is not defined

%timeit は変数を中に閉じ込めるようで、 Out のセルに出力することはできないようでした。

cell mode

次のような記述をして、

%%timeit
i = 0
for x in range(0, 1000000):
    i += 1
i

実行すると、

# 98.8 ms ± 2.32 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

cell mode も変数を中に閉じ込めるようで、 Out のセルに出力することはできないようでした。

終わり

他にもマジックコマンドがあったので、うまく使っていきたいです。

参考