이 예제에서는 Stopwatch 클래스를 사용하여 PLINQ 쿼리를 실행하는 데 걸리는 시간을 측정하는 방법을 보여줍니다.
예시
이 예제에서는 빈 foreach
루프(Visual Basic에서는For Each
)를 사용하여 쿼리를 실행하는 데 걸리는 시간을 측정합니다. 실제 코드에서 루프는 일반적으로 총 쿼리 실행 시간에 추가하는 추가 처리 단계를 포함합니다. 스톱워치는 루프 바로 직전에 시작됩니다. 왜냐하면 쿼리 실행이 시작되는 시점이기 때문입니다. 더 세분화된 측정이 필요한 경우 ElapsedMilliseconds
대신 ElapsedTicks
속성을 사용할 수 있습니다.
using System;
using System.Diagnostics;
using System.Linq;
class ExampleMeasure
{
static void Main()
{
var source = Enumerable.Range(0, 3000000);
var queryToMeasure =
from num in source.AsParallel()
where num % 3 == 0
select Math.Sqrt(num);
Console.WriteLine("Measuring...");
// The query does not run until it is enumerated.
// Therefore, start the timer here.
var sw = Stopwatch.StartNew();
// For pure query cost, enumerate and do nothing else.
foreach (var n in queryToMeasure) { }
sw.Stop();
long elapsed = sw.ElapsedMilliseconds; // or sw.ElapsedTicks
Console.WriteLine($"Total query time: {elapsed} ms");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
Module ExampleMeasure
Sub Main()
Dim source = Enumerable.Range(0, 3000000)
' Define parallel and non-parallel queries.
Dim queryToMeasure = From num In source.AsParallel()
Where num Mod 3 = 0
Select Math.Sqrt(num)
Console.WriteLine("Measuring...")
' The query does not run until it is enumerated.
' Therefore, start the timer here.
Dim sw = Stopwatch.StartNew()
' For pure query cost, enumerate and do nothing else.
For Each n As Double In queryToMeasure
Next
sw.Stop()
Dim elapsed As Long = sw.ElapsedMilliseconds ' or sw.ElapsedTicks
Console.WriteLine($"Total query time: {elapsed} ms.")
Console.WriteLine("Press any key to exit.")
Console.ReadKey()
End Sub
End Module
총 실행 시간은 쿼리 구현을 실험할 때 유용한 메트릭이지만 항상 전체 스토리를 전달하지는 않습니다. 쿼리 스레드가 서로 상호 작용하고 다른 실행 중인 프로세스와 상호 작용하는 것을 더 깊고 풍부하게 보려면 동시성 시각화 도우미사용합니다.
참고하십시오
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET