メソッドを含む配列を作成すると、stopwatch.elapsedMilliseconds は常に 0 を返します。
例:
int[] methods = {method1(), method2()};
Stopwatch sw = new Stopwatch();
sw.Start();
int val = methods[1];
sw.Stop();
Console.WriteLine("It took {0} ms", sw.ElapsedMilliseconds);
// Output: "It took 0 ms"
メソッドを直接呼び出すと、ストップウォッチが正しく機能します。
Stopwatch sw = new Stopwatch();
sw.Start();
method1();
sw.Stop();
Console.WriteLine("It took {0} ms", sw.ElapsedMilliseconds);
// Output: "It took x ms"
私は何を間違っていますか?
編集:実際のメインコード:
static void Main(string[] args)
{
Stopwatch t = new Stopwatch();
Func<int>[] problems = new Func<int>[] { problem5, problem6 };
for (int i = 0; i < problems.Length; i++)
{
t.Restart();
Console.WriteLine("Solution to {0} is: {1}", problems[i].Method.Name , problems[i]());
t.Stop();
Console.WriteLine("It took {0} ms ", t.ElapsedMilliseconds);
}
Console.ReadKey();
}
出力: