英語版
このページの英語版を見る

std.datetime.stopwatch

基本的なベンチマークとタイミング機能を含むモジュール。
便宜上、このモジュールは一般にインポートしている core.time.
カテゴリー 関数
主な機能 StopWatch benchmark
フラグ AutoStart
std.datetime.packageの他のモジュールとは異なり、このモジュールは現在、一般にインポートされていない。 このモジュールは、std.datetime.packageでは一般にインポートされていない。 を使用するこの関数の古いバージョンはstd.datetime.packageにあり、このモジュールは現在std.datetime.packageにはインポートされていない。 core.time.TickDuration を使うこの機能の古いバージョンはstd.datetime.packageにあり、このモジュールのシンボルと衝突してしまうからだ。 このモジュールのシンボルと衝突してしまうからである。古いシンボルが廃止サイクルを経て 非推奨サイクルを経て完全に削除された後、このモジュールは このモジュールはstd.datetime.packageに一般にインポートされる。古い非推奨シンボルは 古い非推奨シンボルは、2019年12月にドキュメントから削除された。 2019年12月にドキュメントから削除され、現在 "phobos"から完全に削除される予定である。 から完全に削除される予定である。
従って、今のところ、std.datetime.stopwatchを使う場合、もし std.datetime.stopwatchを使用する場合、std.datetime.stopwatchの他のモジュールが必要な場合は、std.datetime.stopwatchをインポートするのではなく、個別にインポートする必要がある。 std.datetime.stopwatchをインポートするのではなく、個別にインポートするか、選択インポートまたは静的インポートを使用する。 std.datetime.stopwatchをインポートする。
import std.datetime;
import std.datetime.stopwatch : benchmark, StopWatch;
そうすれば、コンパイラーは、std.datetime.packageの非推奨シンボルではなく、std.datetime.stopwatchのシンボルを使うことがわかる。 のシンボルを使うことがわかる。
Authors:
Jonathan M Davis and Kato Shoichi
alias AutoStart = std.typecons.Flag!"autoStart".Flag;
StopWatchが、構築時に即座に開始するかどうかを示すために使用する。 を示す。
もし AutoStart.noに設定された場合、StopWatchは構築時に開始されない。 を開始しない。
そうでない場合 AutoStart.yesに設定されている場合、ストップウォッチは が開始される。
struct StopWatch;
StopWatchは、物理的なストップウォッチと同じように時間を計測するために使用される。 ストップウォッチは、ストップ、リスタート、リセットを含む、物理的なストップウォッチと同じように時間を計測するために使用される。
core.time.MonoTimeは時間を保持するために使用され、システムの モノトニッククロックは高精度で、決して逆算しない(壁掛け時計のように時間を逆算できるのとは違う)。 壁掛け時計の時間は逆算できるstd.datetime.systime.SysTime壁掛け時計の時刻とは異なり、逆算することができる。)
StopWatchの精度はシステムによって異なることに注釈:。すべてのシステムで同じ精度にすることは不可能である。 というのも、システムクロックの精度や、その他のシステムに依存する要因、状況に依存する要因があるからである。 システムクロックの精度や、その他のシステム依存および状況依存の要因(スレッド間のコンテキストスイッチのオーバーヘッドなど)は、システムによって異なるため、すべてのシステムで同じにすることは不可能である。 (スレッド間のコンテキストスイッチのオーバーヘッドなど)はシステムによって異なるため、すべてのシステムで同じ精度にすることは不可能である。 によって異なり、StopWatch の精度に影響を与える可能性があるからである。
Examples:
ミリ秒、マイクロ秒、ナノ秒単位で時間を計測する
auto sw = StopWatch(AutoStart.no);
sw.start();
// ... ここに時間を計測する操作を挿入する ...
sw.stop();

long msecs = sw.peek.total!"msecs";
long usecs = sw.peek.total!"usecs";
long nsecs = sw.peek.total!"nsecs";

assert(usecs >= msecs * 1000);
assert(nsecs >= usecs * 1000);
Examples:
import core.thread : Thread;

auto sw = StopWatch(AutoStart.yes);

Duration t1 = sw.peek();
Thread.sleep(usecs(1));
Duration t2 = sw.peek();
assert(t2 > t1);

Thread.sleep(usecs(1));
sw.stop();

Duration t3 = sw.peek();
assert(t3 > t2);
Duration t4 = sw.peek();
writeln(t3); // t4

sw.start();
Thread.sleep(usecs(1));

Duration t5 = sw.peek();
assert(t5 > t4);

// StopWatchの停止やリセットが必要ない場合、
// MonoTimeはStopWatchなしで単独で簡単に使用できる。
auto before = MonoTime.currTime;
// 何かをする...
auto timeElapsed = MonoTime.currTime - before;
nothrow @nogc @safe this(AutoStart autostart);
StopWatch を構築する。すぐに開始するかどうかは AutoStart引数に依存する。
StopWatch.init が使用された場合、構築された StopWatch は実行されていない(コンストラクタが実行されていないため、実行できない)。 は実行されていない(コンストラクタが実行されていないので、実行できない)。
Examples:
import core.thread : Thread;

{
    auto sw = StopWatch(AutoStart.yes);
    assert(sw.running);
    Thread.sleep(usecs(1));
    assert(sw.peek() > Duration.zero);
}
{
    auto sw = StopWatch(AutoStart.no);
    assert(!sw.running);
    Thread.sleep(usecs(1));
    writeln(sw.peek()); // Duration.zero
}
{
    StopWatch sw;
    assert(!sw.running);
    Thread.sleep(usecs(1));
    writeln(sw.peek()); // Duration.zero
}

writeln(StopWatch.init); // StopWatch(AutoStart.no)
assert(StopWatch.init != StopWatch(AutoStart.yes));
nothrow @nogc @safe void reset();
ストップウォッチをリセットする。
ストップウォッチは作動中にリセットでき、作動中にリセットしても停止しない。 リセットしても停止しない。
Examples:
import core.thread : Thread;

auto sw = StopWatch(AutoStart.yes);
Thread.sleep(usecs(1));
sw.stop();
assert(sw.peek() > Duration.zero);
sw.reset();
writeln(sw.peek()); // Duration.zero
nothrow @nogc @safe void start();
ストップウォッチを開始する。
startは、StopWatchがすでに実行されている場合は、コールされるべきではない。
Examples:
import core.thread : Thread;

StopWatch sw;
assert(!sw.running);
writeln(sw.peek()); // Duration.zero
sw.start();
assert(sw.running);
Thread.sleep(usecs(1));
assert(sw.peek() > Duration.zero);
nothrow @nogc @safe void stop();
ストップウォッチを停止する。
StopWatch が実行されていない場合は、stop を呼んではならない。
Examples:
import core.thread : Thread;

auto sw = StopWatch(AutoStart.yes);
assert(sw.running);
Thread.sleep(usecs(1));
immutable t1 = sw.peek();
assert(t1 > Duration.zero);

sw.stop();
assert(!sw.running);
immutable t2 = sw.peek();
assert(t2 >= t1);
immutable t3 = sw.peek();
writeln(t2); // t3
const nothrow @nogc @safe Duration peek();
ストップウォッチの作動時間を覗いてみよう。
これは StopWatch が停止していた時間は含まれないが、StopWatch が作動していた時間は全て含まれる。 この時間にはストップウォッチが停止していた時間は含まれない。 を含む。
を呼び出すと、これはリセットされる。 resetを呼び出すと、これはDuration.zero にリセットされる。
Examples:
import core.thread : Thread;

auto sw = StopWatch(AutoStart.no);
writeln(sw.peek()); // Duration.zero
sw.start();

Thread.sleep(usecs(1));
assert(sw.peek() >= usecs(1));

Thread.sleep(usecs(1));
assert(sw.peek() >= usecs(2));

sw.stop();
immutable stopped = sw.peek();
Thread.sleep(usecs(1));
writeln(sw.peek()); // 停止した

sw.start();
Thread.sleep(usecs(1));
assert(sw.peek() > stopped);
nothrow @nogc @safe void setTimeElapsed(Duration timeElapsed);
StopWatchが実行されている合計時間を設定する。 が返す時間)を設定する。
setTimeElapsed が呼び出されるために、StopWatch が停止している必要はない。 を呼び出すために、StopWatch が停止している必要はなく、呼び出すことによって StopWatch が停止することもない。
Examples:
import core.thread : Thread;

StopWatch sw;
sw.setTimeElapsed(hours(1));

// MonoTimeのドキュメントで説明されているように、
// Durationとticksの変換は正確ではないが、近い値にはなる。
// どの程度正確かは、システムのモノトニッククロックの
// 周波数/分解能に依存する。
assert(abs(sw.peek() - hours(1)) < usecs(1));

sw.start();
Thread.sleep(usecs(1));
assert(sw.peek() > hours(1) + usecs(1));
const pure nothrow @nogc @property @safe bool running();
このストップウォッチが現在実行中であるかどうかを返す。
Examples:
StopWatch sw;
assert(!sw.running);
sw.start();
assert(sw.running);
sw.stop();
assert(!sw.running);
Duration[fun.length] benchmark(fun...)(uint n);
速度評価と比較のためのベンチマークコード。
Parameters:
fun 呼び出し可能オブジェクトのエイリアス(関数名など)。呼び出し可能な オブジェクトは引数をとらない。
uint n 各関数の実行回数。
Returns:
各関数の実行回数。 core.time.Durationとして)。 各関数を呼び出すのにかかった時間である。 n回である。最初の値は、次の関数を呼び出すのにかかった時間の長さである。 fun[0] を呼び出すのにかかった時間の長さである。 n回呼び出すのにかかった時間の長さである。番目の値は、 を呼び出すのにかかった時間の長さである。 fun[1]n回である。などである。
Examples:
import std.conv : to;

int a;
void f0() {}
void f1() { auto b = a; }
void f2() { auto b = to!string(a); }
auto r = benchmark!(f0, f1, f2)(10_000);
Duration f0Result = r[0]; // f0が10,000回実行されるのに要した時間
Duration f1Result = r[1]; // f1が10,000回実行されるのに要した時間
Duration f2Result = r[2]; // f2が10,000回実行されるのに要した時間