System.Diagnostics.Stopwatch
コンピューターの待機中に時間をカウントしますか?
2 に答える
3
実際、少しテストしたところ、カウントされているように見えますが、かなり外れていました。これは私がしたことです:
- 以下のコードを書きました
- それを実行し、すぐにコンピューターをスリープモードにしました
- 10 秒待って、コンピューターを元に戻しました
Elapsed
その結果、4分を超える衝撃のタイムを記録。離れて。したがって、これをいかなる種類のベンチマークとしても使用しません。
使用したコードは次のとおりです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace TestingCode
{
class Program
{
static void Main(string[] args)
{
Stopwatch testing = new Stopwatch();
testing.Start();
Console.WriteLine("Press any key to stop timer");
Console.ReadKey();
testing.Stop();
Console.WriteLine("Elapsed: {0}", testing.Elapsed);
Console.WriteLine("Done!!");
Console.ReadKey();
}
}
}
于 2011-12-28T13:26:48.520 に答える
3
はい、そうです。
Reflector のコードを見ると、 でない場合Stopwatch.IsHighResolution
はティック カウントが使用されることがわかります (私の環境では、値が だっfalse
たので を使用しますDateTime.UtcNow.Ticks
)。
public void Start()
{
if (!this.isRunning)
{
this.startTimeStamp = GetTimestamp();
this.isRunning = true;
}
}
public static long GetTimestamp()
{
if (IsHighResolution)
{
long num = 0L;
SafeNativeMethods.QueryPerformanceCounter(out num);
return num;
}
return DateTime.UtcNow.Ticks;
}
于 2011-12-28T13:27:40.773 に答える