これは何か他のものです。 サブネットに精通している人は誰でも、 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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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でこのフレームワークを開いて、お互いを愛してください。 その後、パフォーマンスがあります。