public void Randomize()
        {
            for (int count = 0; count < 10; count++)
            {
                int[] list = CollectionsUtility.IndexArray(count);
                CollectionsUtility.Randomize(list);

                // check that all elements still occur exactly once
                bool[] found = new bool[count];
                for (int i = 0; i < list.Length; i++)
                {
                    Assert.IsFalse(found[list[i]]);
                    found[list[i]] = true;
                }
                for (int i = 0; i < list.Length; i++)
                {
                    Assert.IsTrue(found[list[i]]);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a random <see cref="Subdivision"/> with the specified number of full edges and
        /// comparison epsilon.</summary>
        /// <param name="size">
        /// The number of full edges, i.e. half the number of <see cref="Subdivision.Edges"/>, in
        /// the returned <see cref="Subdivision"/>.</param>
        /// <param name="epsilon">
        /// The maximum absolute difference at which two coordinates should be considered equal.
        /// </param>
        /// <returns>
        /// A new random <see cref="Subdivision"/> with the specified <paramref name="size"/> and
        /// <paramref name="epsilon"/>.</returns>

        private static Subdivision CreateSubdivision(int size, double epsilon)
        {
            LineD[] lines = new LineD[size];
            for (int i = 0; i < size; i++)
            {
                lines[i] = GeoAlgorithms.RandomLine(0, 0, 1000, 1000);
            }

            // split random set into non-intersecting line segments
            var crossings  = MultiLineIntersection.FindSimple(lines, epsilon);
            var splitLines = MultiLineIntersection.Split(lines, crossings);

            Array.Copy(splitLines, lines, size);

            // re-randomize lines to eliminate split ordering
            CollectionsUtility.Randomize(lines);
            Subdivision division = Subdivision.FromLines(lines);

            division.Validate();
            return(division);
        }
Ejemplo n.º 3
0
        private void DictionaryTest(bool random)
        {
            Stopwatch timer     = new Stopwatch();
            var       testCases = _dictionaryTestCases;

            Output(String.Format("{0,8}", " "));
            foreach (TestCase test in testCases)
            {
                Output(String.Format("{0,14}", test.Name));
            }
            Output("\n");

            // count units of size x operation in milliseconds,
            // rather than individual operations in microseconds
            const int outerLoop = 100, size = 60000;
            const int iterations = outerLoop * 1000;

            long[] addTicks = new long[testCases.Length],
            iterateTicks = new long[testCases.Length],
            searchTicks  = new long[testCases.Length],
            removeTicks  = new long[testCases.Length];

            // generate keys in ascending order
            var array = new KeyValuePair <Int32, String> [size];

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = new KeyValuePair <Int32, String>(i, null);
            }

            // create random permutation of keys
            if (random)
            {
                CollectionsUtility.Randomize(array);
            }

            // trigger JIT compilation
            foreach (TestCase test in testCases)
            {
                test.Dictionary.Clear();
                foreach (var pair in array)
                {
                    test.Dictionary.Add(pair.Key, pair.Value);
                }
            }

            for (int i = 0; i < outerLoop; i++)
            {
                for (int j = 0; j < testCases.Length; j++)
                {
                    TestCase test = testCases[j];
                    test.Dictionary.Clear();

                    timer.Restart();
                    foreach (var pair in array)
                    {
                        test.Dictionary.Add(pair.Key, pair.Value);
                    }
                    timer.Stop();
                    addTicks[j] += timer.ElapsedTicks;

                    int sum = 0;
                    timer.Restart();
                    foreach (var pair in test.Dictionary)
                    {
                        unchecked { sum += pair.Key; }
                    }
                    timer.Stop();
                    iterateTicks[j] += timer.ElapsedTicks;

                    int key = MersenneTwister.Default.Next(size - 1);
                    timer.Restart();
                    for (int k = 0; k < size; k++)
                    {
                        test.Dictionary.ContainsKey((key + k) % size);
                    }
                    timer.Stop();
                    searchTicks[j] += timer.ElapsedTicks;

                    timer.Restart();
                    foreach (var pair in array)
                    {
                        test.Dictionary.Remove(pair.Key);
                    }
                    timer.Stop();
                    removeTicks[j] += timer.ElapsedTicks;
                }
            }

            Output(String.Format("{0,8}", "Add"));
            for (int i = 0; i < testCases.Length; i++)
            {
                Output(String.Format("{0,14:N2}", AverageMicrosecs(addTicks[i], iterations)));
            }

            Output(String.Format("\n{0,8}", "Iterate"));
            for (int i = 0; i < testCases.Length; i++)
            {
                Output(String.Format("{0,14:N2}", AverageMicrosecs(iterateTicks[i], iterations)));
            }

            Output(String.Format("\n{0,8}", "Search"));
            for (int i = 0; i < testCases.Length; i++)
            {
                Output(String.Format("{0,14:N2}", AverageMicrosecs(searchTicks[i], iterations)));
            }

            Output(String.Format("\n{0,8}", "Remove"));
            for (int i = 0; i < testCases.Length; i++)
            {
                Output(String.Format("{0,14:N2}", AverageMicrosecs(removeTicks[i], iterations)));
            }

            Output(String.Format("\n\nTimes are msec averages for {0:N0} integer keys in {1} order.\n",
                                 size, (random ? "random" : "ascending")));
        }
Ejemplo n.º 4
0
        private void CollectionTest()
        {
            Stopwatch timer     = new Stopwatch();
            var       testCases = _collectionTestCases;

            Output(String.Format("{0,8}", " "));
            foreach (TestCase test in testCases)
            {
                Output(String.Format("{0,14}", test.Name));
            }
            Output("\n");

            // count units of size x operation in milliseconds,
            // rather than individual operations in microseconds
            const int outerLoop = 100, size = 200000;
            const int iterations = outerLoop * 1000;

            long[] addTicks = new long[testCases.Length],
            iterateTicks = new long[testCases.Length],
            searchTicks  = new long[testCases.Length],
            removeTicks  = new long[testCases.Length];

            // generate random permutation of items
            int[] array = CollectionsUtility.IndexArray(size);
            CollectionsUtility.Randomize(array);

            // trigger JIT compilation
            foreach (TestCase test in testCases)
            {
                test.Collection.Clear();
                foreach (int item in array)
                {
                    test.Collection.Add(item);
                }
            }

            for (int i = 0; i < outerLoop; i++)
            {
                for (int j = 0; j < testCases.Length; j++)
                {
                    TestCase test = testCases[j];
                    test.Collection.Clear();

                    timer.Restart();
                    foreach (int item in array)
                    {
                        test.Collection.Add(item);
                    }
                    timer.Stop();
                    addTicks[j] += timer.ElapsedTicks;

                    int sum = 0;
                    timer.Restart();
                    foreach (int item in test.Collection)
                    {
                        unchecked { sum += item; }
                    }
                    timer.Stop();
                    iterateTicks[j] += timer.ElapsedTicks;

                    int key = MersenneTwister.Default.Next(size - 1);
                    timer.Restart();
                    for (int k = 0; k < size; k++)
                    {
                        test.Collection.Contains((key + k) % size);
                    }
                    timer.Stop();
                    searchTicks[j] += timer.ElapsedTicks;

                    timer.Restart();
                    foreach (int item in array)
                    {
                        test.Collection.Remove(item);
                    }
                    timer.Stop();
                    removeTicks[j] += timer.ElapsedTicks;
                }
            }

            Output(String.Format("{0,8}", "Add"));
            for (int i = 0; i < testCases.Length; i++)
            {
                Output(String.Format("{0,14:N2}", AverageMicrosecs(addTicks[i], iterations)));
            }

            Output(String.Format("\n{0,8}", "Iterate"));
            for (int i = 0; i < testCases.Length; i++)
            {
                Output(String.Format("{0,14:N2}", AverageMicrosecs(iterateTicks[i], iterations)));
            }

            Output(String.Format("\n{0,8}", "Search"));
            for (int i = 0; i < testCases.Length; i++)
            {
                Output(String.Format("{0,14:N2}", AverageMicrosecs(searchTicks[i], iterations)));
            }

            Output(String.Format("\n{0,8}", "Remove"));
            for (int i = 0; i < testCases.Length; i++)
            {
                Output(String.Format("{0,14:N2}", AverageMicrosecs(removeTicks[i], iterations)));
            }

            Output(String.Format(
                       "\n\nTimes are msec averages for {0:N0} integer items in random order.\n", size));
        }