0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ユニットテストは大抵の場合、 特定のクラスやメソッドを直接実行できる ようになっています。
このことは以下の使い方にも応用できます。

  1. 実行時間測定
  2. メモリやCPU使用量の測定
  3. マニュアルや仕様書の元ネタ取得

1. 実行時間測定

どのアルゴリズムでやると早いかなどを比較検討する際に使えそうです。
ただし、実行時間そのものは、実行環境に依存しますので、
実行時間で assert するのはお勧めしません。

// 順番に足していく
int calc_total_1(int max)
{
    int total = 0;
    for (int i = 1; i <= max; i++) {
        total += i;
    }
    return total;
}

// 公式を使う
int calc_total_2(int max)
{
    return max * (max + 1) / 2;
}
// テストコード、ならぬ、実行時間測定コード
void test_measure_calc_total() {
    // calc_total_1の実行時間を計測
    ...

    // calc_total_2の実行時間を計測
    ...

    // 動作環境に依存するので、こういうassertはお勧めしない
    // ただし、本番環境に近い環境で動かせる場合は入れてもいいかも
    assert(実行時間 < 10000);
}

2. メモリやCPU使用量の測定

特定の処理を実行する前後のメモリ使用量やCPU使用量を測定して、増加量やメモリリークなどを調べることができます。もちろん、直接数値を見て判断するのが難しいことは言うまでもありませんが。
(メモリはすぐには解放されなかったり、ピーク使用量は見れなかったり)

3. マニュアルや仕様書の元ネタ取得

以下のようなことができそうです。

  • マニュアル用のスクリーンショット作成
  • ログのサンプル出力
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?