C#のハッシュセット反復アルゴリズムの複雑さ

Reflectorで.NETフレームワーククラスの内部を調べると、多くの興味深いことがわかります。 たとえば、ある日(かなり前)、Donnet QuickSortの場合、2次時間でソートが実行される入力データのクラスを非常に簡単に思い付くことができることがわかりました。 したがって、「ピラミッド」配列(つまり、最大要素が中央、2番目と3番目に大きい要素-左右など)でのクイックソートは、バブルソートに匹敵します。 理由は明らかです。配列の中央の要素が中央値として選択されました。 この問題がどのように発生したかはわかりませんが、まだ発生する場合は、別の投稿に値します。



これは何か他のものです。 サブネットに精通している人は誰でも、 System.Collections.Genericクラスを知っていると思います ハッシュセット<T> 。 MSDNによると、HashSetクラスはセットで高性能な操作を提供します。 たとえば、1つの要素からのハッシュセットでコンピューターが200回実行される時間はどのくらいだと思いますか? 100分の1秒ですか? 少ない? まったく事実ではありません!



実際には、ハッシュセットの内部デバイスは、削除された要素の代わりに「穴」が表示されるようになっているため、イテレーターが実行される特別な方法でマークされた要素です。 したがって、たとえば、最初にハッシュセットに100万個の要素を入れてから、そのほとんどすべてを削除して、ほんの数個を残した場合、これらの残りの要素の繰り返しに費やす時間ははるかに長くなり、サイズの比較可能なハッシュセットを実行します削除操作は実行されませんでした。



次のコードは、困難な過去のないハッシュセットの反復時間が、最初に100万個の要素がスローされた後、ほとんどすべてが使用されたボロボロのハッシュセットの反復の時間とどのように異なるかを示しています。



using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  1. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  2. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  3. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  4. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  5. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  6. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  7. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  8. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  9. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  10. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  11. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  12. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  13. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  14. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  15. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  16. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  17. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  18. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  19. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  20. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  21. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  22. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  23. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  24. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  25. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  26. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  27. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  28. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  29. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  30. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  31. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  32. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  33. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  34. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  35. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  36. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



  37. using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .



using System; using System.Collections. Generic ; using System.Diagnostics; public class HashSetIterationPerformance { public const int MaxItemsCount = 10000000; public const int RepeatIterations = 200; public static TimeSpan MeasureIterationTime(HashSet< int > hashSet) { var stopwatch = new Stopwatch(); stopwatch.Start(); for ( int i = 0; i < RepeatIterations; ++i) foreach ( var x in hashSet) { // Do nothing } return new TimeSpan (stopwatch.Elapsed.Ticks / RepeatIterations); } public static void Main( string [] args) { var hashSet = new HashSet< int >(); hashSet.Add(0); Console .WriteLine( "Usual scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); hashSet = new HashSet< int >(); // - : [0..MaxItemsCount) for ( int i = 0; i < MaxItemsCount; ++i) { hashSet.Add(i); } // , for ( int i = 1; i < MaxItemsCount; ++i) { hashSet.Remove(i); } Console .WriteLine( "Add/Remove scenario. Iteration time: {0}" , MeasureIterationTime(hashSet)); } } * This source code was highlighted with Source Code Highlighter .







私のマシンでは、これが表示されます:



Usual scenario. Iteration time: 00:00:00.0000032

Add/Remove scenario. Iteration time: 00:00:00.0468619








ご覧のとおり、時間差は非常に大きく、何千回も機能しますが、機能的には両方のケースで同じアクションを実行しましたが、1つの要素のセットにまたがりました。



厳密に言えば、最悪の場合のDonnet HashSetのすべての要素を反復処理する時間的な複雑さはO(N)ではなく、Nはその中の要素の数であり、O(ハッシュ内のこれまでにあった要素の最大数)です。



だから、友人、結論は簡単です。 信じられないほどMSDNを読み、.NETクラスを注意して使用し、Reflectorでこのフレームワークを開いて、お互いを愛してください。 その後、パフォーマンスがあります。



All Articles