実行時間と計測

Julia中級

Anthony Markham

Quantitative Developer

初期の関数定義

  • 問題には複数の解があるが、「最適」解があることが多い
  • 大きなメモリ使用量や長い実行時間はユーザー/開発者の不満につながる
  • ループは実行時間の増加と非効率の主因
function my_function()
  x = Vector{Char}()
  for i in "Anthony"
    push!(x, i)
  end
  println(x)
end
['A', 'n', 't', 'h', 'o', 'n', 'y']
Julia中級

Base の @time マクロ

  • 関数の計測にはマクロを使います。

利点:

  • Julia Base に含まれる
  • 呼び出しが簡単
  • 出力が解釈しやすい

欠点:

  • 柔軟性が低い
  • 1 回しか計測しない
  • 最初の呼び出しはコンパイルのオーバーヘッドあり
  • 関数にマクロを適用
@time my_function()
@time my_function()
  • 返り値

0.278266 seconds (110.66 k allocations: 6.343 MiB, 99.79% compilation time)

0.000493 seconds (100 allocations: 3.281 KiB)

Julia中級

BenchmarkTools - @benchmark

なぜ @time より @benchmark?

  • 最も柔軟。samples など多くのパラメータを調整可能
  • 実行時間の詳細統計(範囲・中央値・平均)
@benchmark my_function

Julia中級

演習に進みましょう!

Julia中級

Preparing Video For Download...