2016-04-10 11 views
0

SoWarum ist meine ElapsedMilliseconds hier immer Null?

 Stopwatch Watch = new Stopwatch(); 
     long tList = 0, tHset = 0; // ms 
     foreach (string Str in Copy) 
     { 
      // measure time to look up string in ordinary list 
      Watch.Start(); 
      if (ListVersion.Contains(Str)) { } 
      Watch.Stop(); 
      tList += Watch.ElapsedMilliseconds; 
      // now measure time to look up same string in my hash set 
      Watch.Reset(); 
      Watch.Start(); 
      if (this.Contains(Str)) { } 
      Watch.Stop(); 
      tHset += Watch.ElapsedMilliseconds; 
      Watch.Reset(); 
     } 
     int n = Copy.Count; 
     Console.WriteLine("Average milliseconds to look up in List: {0}", tList/n); 
     Console.WriteLine("Average milliseconds to look up in hashset: {0}", tHset/n); 

es outputing 0 sowohl im Vergleich zu der Leistung der gleichen Elemente in einem List und im folgenden Code-Block erstellt Ich versuche, um die Leistung des Hash-Sets zu messen ich. Irgendeine Idee warum das ist? Relevante Dokumentation: https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx

+2

haben Die Vorgänge sind zu schnell und Sie sollten die Leistung nicht so messen. –

Antwort

3

Das ist, weil die Operation schneller ist als die Präzision der Stapwatch.

Statt jedes der Contains Anruf Messung separat eine Gruppe von ihnen messen:

Stopwatch Watch = new Stopwatch(); 
long tList = 0, tHset = 0; // ms 

// measure time to look up string in ordinary list 
Watch.Start(); 
foreach (string Str in Copy) 
{ 
    if (ListVersion.Contains(Str)) { } 
} 
Watch.Stop(); 
tList = Watch.ElapsedMilliseconds; 
// now measure time to look up same string in my hash set 
Watch.Reset(); 
Watch.Start(); 
foreach (string Str in Copy) 
{ 
    if (this.Contains(Str)) { } 
} 
Watch.Stop(); 
tHset = Watch.ElapsedMilliseconds; 

Console.WriteLine("Total milliseconds to look up in List: {0}", tList); 
Console.WriteLine("Total milliseconds to look up in hashset: {0}", tHset); 

Wie Sie sehen können, Ich habe auch den Code Gesamtzeit statt Durchschnitt ausgegeben zu drucken. Bei Operationen wird die Performance normalerweise in X pro Y-Operation anstatt im Durchschnitt dargestellt. Z.B. 40ms pro 10 Millionen Suchvorgänge.

Auch ist es möglich, dass im Freigabemodus Teile Ihres Codes weg optimiert werden, weil es eigentlich nichts macht. Zählen Sie die Anzahl der Elemente, für die Contains den Wert "true" zurückgibt, und drucken Sie diese Zahl am Ende aus.

+0

Huh? Mein Compiler wäre blöd, wenn er annimmt, dass meine 'if'-Anweisungen im Programm – user6048670

+2

@ user6048670 nichts bewirken. Aber dein if tut nichts. Wie ist der Compiler dumm wenn er das erkennt? – Paparazzi

+0

Es müsste ein sehr intelligenter Compiler sein, um zu verstehen, dass ein Aufruf der 'Contains'-Methode des Containers keine Nebenwirkungen hat. Ich bezweifle, dass die Assembly-Metadaten genügend Informationen enthalten, um dem Compiler so etwas mitzuteilen. –

0

Sie Ihren Code halten, wie es ist, und anstatt das zu tun:

Watch.ElapsedMilliseconds 

Sie tun dies:

Watch.Elapsed.TotalMilliseconds 

diese Weise können Sie den Bruchteil der Millisekunde Da