2014-09-03 3 views
0

동일한 작업을 수행하는 두 개의 서로 다른 라이브러리에서 작업하고 있습니다. 주어진 입력 세트로 어떤 라이브러리가 동일한 작업을 더 빠르게 수행하는지 확인하도록 지정되었습니다. 이 테스트를 위해 여러 가지 테스트 케이스가 있습니다. 라이브러리에서 작업을 수행하는 데 걸린 시간 (밀리 초)을 확인하고 싶습니다.C# 코드의 성능을 확인하는 가장 좋은 방법

다음은 코드의 개요입니다. 나는 C#에서 그것을 할 수 있겠

// preparation of input parameters 
// Get the timestamp 
// Calling the desired library with the set of input parameters 
// again get the timestamp 
// Process the output received from the library 

// get a diff between timestamp and write it into a file. 

, 일부 제공되는 문서와 난 (밀리 초에) 걸리는 시간

int s1 = DateTime.Now.Millisecond; 
int e1 = DateTime.Now.Millisecond; 
int f1 = e1 - s1; 
FileStream fs = new FileStream("Time_Diff.txt", FileMode.Append); 
StreamWriter sw = new StreamWriter(fs); 
string str = Convert.ToString(f1); 
sw.WriteLine(str); 
sw.WriteLine(e1 + " " + s1); 
sw.Flush(); 
sw.Close(); 
을 측정하는 데 사용할 수있는 다음 코드를 내놓았다 일부 온라인 검색을 읽기에

저는 올바른 방향으로 가고 있다는 것을 알고 싶었습니다. 또한 누군가 나에게 똑같은 일을하는 또 다른 방법을 제안 할 수 있다면?

내가 알고 싶은 또 다른 사실은 우리가 선택한 라이브러리가 최상의 성능을 발휘할 수 있도록하는 데 사용할 수있는 다른 실질적인 승인입니까?

다른 작은 요청. 누군가가 C 나에서 같은 일을 스톱워치 (또는 타임 스탬프를 얻을 다른 비슷한 방법)을 구현하는 방법을 알려 주시기 바랍니다 수 있습니까?

감사합니다, 다음과 같이 Azeem

+1

은 diff, 좋은 생각이 아니다 (http://jaychapman.blogspot.co.at/2007/11/datetimenow-precision.html). ..'Stopwatch' 인스턴스를 사용하십시오! 어쨌든 정교한 google-search를 위해서 "benchmark"와 "C#"키워드를 사용하십시오. 아니면 다음의 질문을 읽어보십시오 : http://stackoverflow.com/questions/28637/is-datetime-now 가장 좋은 방법으로 기능 측정 성능 –

+3

['Stopwatch'] (http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch (v = vs. 110) .aspx) 클래스 대신 'DateTime'클래스를 사용해야합니다. [관련 질문] (http://stackoverflow.com/questions/28637/is-datetime-now-the-best-way-to-measure-a-functions-performance). – Yuriy

+0

각 통화는 얼마나 걸리나요? 상당한 시간을 할애해야합니다. 이는 수천 또는 수백만 시간의 통화를 의미 할 수 있습니다. –

답변

2

사용 스톱워치. 정밀도가 좋은 아니므로`DateTime`-인스턴스 사이

 var stopWatch = System.Diagnostics.Stopwatch.StartNew(); 
     FileStream fs = new FileStream("Time_Diff.txt", FileMode.Append); 
     StreamWriter sw = new StreamWriter(fs); 
     //Total time required 
     sw.WriteLine("\nTotal time: " + stopWatch.Elapsed.TotalSeconds.ToString()); 
     sw.Flush(); 
     sw.Close(); 
관련 문제