Exemplo n.º 1
0
        private static void RecordSearchTime(string key, ISearcher <int> searcher, IEnumerable <int> enumerable)
        {
            var hasKey  = _dictionary.ContainsKey(key);
            var results = hasKey ? _dictionary[key] : new List <TimeSpan>();

            IList <int> array = enumerable.ToArray();

            for (var i = 1; i < _iterations + 1; i++)
            {
                Console.WriteLine("Begin Test: {0} v{1}", key, i);

                var targetIndex = _random.Next(0, array.Count);
                var target      = array[targetIndex];
                Console.WriteLine("Target: {0}/{1}, Value: {2}", targetIndex, array.Count, target);

                Console.WriteLine("Starting timer...");
                var timer = new Stopwatch();

                timer.Start();
                var matchIndex = searcher.IndexOf(array, target);
                timer.Stop();

                Console.WriteLine("Time Taken: {0}", GetTime(timer.Elapsed));

                if (matchIndex > -1)
                {
                    Console.WriteLine("First Match: {0}/{1}, Value: {2}", matchIndex, array.Count, array[matchIndex]);
                }
                else
                {
                    Console.WriteLine("First Match: Not Found...");
                }

                // Perform a linear search to find all values
                var expectedIndicies = array
                                       .Select((value, index) => (value, index))
                                       .Where(x => x.value == target)
                                       .Select(x => x.index)
                                       .ToArray();

                if (matchIndex > -1 && expectedIndicies.Contains(matchIndex))
                {
                    Console.WriteLine("Valid Search: Yes");
                    results.Add(timer.Elapsed);
                }
                else
                {
                    Console.WriteLine("Valid Search: No");
                }

                Console.WriteLine("Could have been any of: [{0}]", string.Join(", ", expectedIndicies));


                Console.WriteLine("End Test: {0} v{1}\r\n", key, i);
            }

            if (!hasKey)
            {
                _dictionary.Add(key, results);
            }
        }