Skip to content

Instantly share code, notes, and snippets.

@hebiyan
Created March 18, 2016 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hebiyan/7bb55c0902e799d11462 to your computer and use it in GitHub Desktop.
Save hebiyan/7bb55c0902e799d11462 to your computer and use it in GitHub Desktop.

Standard ML で簡易タイマを書いてみた

概要

time(1) コマンドや、Common Lisp の time 関数 のような、 処理全体にどのくらい時間がかかっているのか教えてくれる関数が Standard ML の標準ライブラリになさそうだった。なので自分でいい加減なものを書いてみた。

幸い、実行時間を測るタイマ自体は標準ライブラリが提供してくれていた。 なので「書いてみた」といっても、標準ライブラリの呼び出しているだけ。

コード

fun time f = let
    val realt = Timer.startRealTimer()
    val rv = f ()
    val elapsed = Timer.checkRealTimer realt
in
    (Time.toMilliseconds elapsed, Time.toMicroseconds elapsed, rv)
end;

使い方

これは、こんな感じで使う。

fun hoge arg1 arg2 = ...;

val (milli, micro, rv) = time (fn () => hoge n m);

このように、測りたい関数を呼び出す、引数のない関数を time に渡すだけである。 time 関数は

  • 関数を処理するのにかかった時間 (単位: ミリ秒)
  • 同上 (単位: マイクロ秒)
  • 関数の戻り値

の三つ組を返す。

ここまで

使い方の具体例とか必要?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment