[C#] Stopwatch 사용 ( 시간체크, 동작시간 )
본문
. Stopwatch 사용
: Stopwatch 를 사용하여 프로그램 동작시간을 확인 합니다.
* 코드 확인
// Stopwatch 객체를 생성 합니다.
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
// Stopwatch 를 시작 합니다.
sw.Start();
sw.Start();
// 숫자를 출력 합니다.
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i.ToString());
}
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i.ToString());
}
// Stopwatch 가 동작 중인지 확인 합니다.
if (sw.IsRunning)
{
if (sw.IsRunning)
{
// Stopwatch 를 종료 합니다.
sw.Stop();
sw.Stop();
// 동작 시간일 밀리초로 출력 합니다.
Console.WriteLine("END TIME :: " + sw.ElapsedMilliseconds.ToString() + " msec");
Console.WriteLine("END TIME :: " + sw.ElapsedMilliseconds.ToString() + " msec");
// 동작 시간을 출력 합니다.
Console.WriteLine("END TIME :: " + sw.Elapsed.ToString());
}
Console.WriteLine("END TIME :: " + sw.Elapsed.ToString());
}
- 동작 확인
2. Stopwatch 응용 사용 예제
: 'stopwatch ok' 란 값을 입력 받는 동작시간을 체크한 후 체크 시간에 따라 성공, 실패 여부를 나타냅니다.
* 코드 확인
while (flag)
{
{
// 키를 입력 받습니다.
result = Console.ReadLine();
result = Console.ReadLine();
// 입력 받은 값이 "stopwatch ok" 인지 확인 합니다.
if (result == "stopwatch ok")
{
if (result == "stopwatch ok")
{
// Stopwatch 가 동작 중인지 확인 합니다.
if (sw.IsRunning)
{
sw.Stop();
if (sw.IsRunning)
{
sw.Stop();
// 동작 시간이 3000msec 이하이면 성공 값을 출력 합니다.
if (sw.ElapsedMilliseconds < (long)3000)
{
Console.WriteLine(sw.ElapsedMilliseconds.ToString() + " msec");
Console.WriteLine("success");
flag = false;
}
if (sw.ElapsedMilliseconds < (long)3000)
{
Console.WriteLine(sw.ElapsedMilliseconds.ToString() + " msec");
Console.WriteLine("success");
flag = false;
}
// 동작 시간이 3000msec 이상이면 실패 값을 출력 합니다.
else
{
Console.WriteLine(sw.ElapsedMilliseconds.ToString() + " msec");
Console.WriteLine("fail");
}
sw.Reset();
sw.Start();
}
}
}
else
{
Console.WriteLine(sw.ElapsedMilliseconds.ToString() + " msec");
Console.WriteLine("fail");
}
sw.Reset();
sw.Start();
}
}
}
- 동작 확인
관련자료
-
링크
-
이전
-
다음
댓글 0개
등록된 댓글이 없습니다.