2017-11-29 2 views
0

모든 방법에 대해 매번이 과정을 거치지 않는 메서드/함수를 깔끔하게 쉽게 프로파일 링하는 방법이 있습니까? 많은 기능을 그가 큰 번거 로움하지 않는 것이 스톱워치로 .NET의 메서드 수준에서 자동으로 프로파일 링

끝에서 stopwatch.Elapsed.TotalMilliseconds으로 결과를 출력하는 말

  • 에서 stopwatch.Stop()를 호출 Dim stopwatch As Stopwatch = Stopwatch.StartNew()
  • 하지만, 그것을 넘어서 반복 : 스톱워치 변수를 선언

    1. dirties 코드를 다소, 그리고 거기에 방법의 시작 부분에 시간을 계산을 시작하고 중지 할 때 자동으로 검색하는 하나의 깨끗한 단계에서 이렇게하는 방법이있을 수 있습니다 궁금 해서요. 나는 그것을 의심하지만 나는 전문가가 아니다.

      감사합니다.

  • 답변

    1

    왜 자신의 도우미 방법을 쓰지 않으시겠습니까? 이처럼 :

    public class TimerLogger : IDisposable 
    { 
        private string _message; 
        private Stopwatch _timer; 
    
        public TimerLogger(string message) 
        { 
         _message = message; 
         _timer = new Stopwatch(); 
         _timer.Start(); 
        } 
    
        public void Dispose() 
        { 
         _timer.Stop(); 
         Console.WriteLine($"Calculation time for {_message}: {_timer.ElapsedMilliseconds}");   
        } 
    } 
    

    사용법 :

    using(new TimerLogger("Test")){ 
        for(int i = 0; i < 1000; i++) 
         Thread.Sleep(5); 
    } 
    
    +0

    대단합니다! 꽤 정확하게 내가 뭘 찾고 있어요. 고맙습니다. – Man

    1

    로마의 응답이 정확히 내가 무엇을 찾고 있었다이지만 C 번호입니다. VB.NET에서 저처럼 누군가 필요한 경우가 있습니다. 또한 도우미가 호출 된 메소드 및 특정 라인에 대한 세부 사항을 출력합니다.

    Public Class Profiler 
    
        Implements IDisposable 
    
        Private ReadOnly _timer As Stopwatch 
    
        Private ReadOnly _methodName As String 
        Private ReadOnly _lineNumber As Integer 
    
        Public Sub New(<System.Runtime.CompilerServices.CallerMemberName> Optional memberName As String = Nothing, 
            <System.Runtime.CompilerServices.CallerLineNumber()> Optional sourceLineNumber As Integer = 0) 
    
         _timer = New Stopwatch() 
         _methodName = memberName 
         _lineNumber = sourceLineNumber 
         _timer.Start() 
    
        End Sub 
    
        Public Sub Dispose() Implements System.IDisposable.Dispose 
    
         _timer.Stop() 
    
         Console.WriteLine("A timer was called in the method " & _methodName & ", line " & _lineNumber & "; the result was " & _timer.Elapsed.Milliseconds & "ms."); 
    
        End Sub 
    
    End Class 
    

    좋은 하루 보내십시오.