Пример #1
0
        [TestMethod, Ignore] //slow test.  no need to run for the moment
        public void DictionaryTiming()
        {
            //this method is slow because of the boxing/unboxing of the enum value (int) to (object)
            var rng            = new Random();
            var statCollection = new Dictionary <AdditiveStat, AdditiveStatClass>();

            var sw = new Stopwatch();

            sw.Start();

            foreach (var stat in Helpers.List)
            {
                var statValue = new AdditiveStatClass {
                    Value = (float)rng.NextDouble()
                };
                statCollection.Add(stat, statValue);
            }

            Console.WriteLine("End of creating stats: " + sw.Elapsed);
            sw.Reset();
            sw.Start();

            for (var i = 0; i < 100000; i++)
            {
                foreach (var stat in Helpers.List)
                {
                    statCollection[stat].Value += (float)rng.NextDouble();
                }
            }

            var sum = 0f;

            foreach (var stat in Helpers.List)
            {
                sum += statCollection[stat].Value;
            }

            Console.WriteLine("sum: " + sum);
            Console.WriteLine("End of summing stats: " + sw.Elapsed);

            sw.Stop();
        }
Пример #2
0
        [TestMethod, Ignore] //slow test.  no need to run for the moment
        public void DictionaryTiming()
        {
            //this method is slow because of the boxing/unboxing of the enum value (int) to (object)
            var rng = new Random();
            var statCollection = new Dictionary<AdditiveStat, AdditiveStatClass>();

            var sw = new Stopwatch();
            sw.Start();

            foreach (var stat in Helpers.List)
            {
                var statValue = new AdditiveStatClass { Value = (float)rng.NextDouble() };
                statCollection.Add(stat, statValue);
            } 
            
            Console.WriteLine("End of creating stats: " + sw.Elapsed);
            sw.Reset();
            sw.Start();

            for ( var i = 0; i < 100000; i++ )
            {
                foreach (var stat in Helpers.List)
                {
                    statCollection[stat].Value += (float)rng.NextDouble();
                }
            }

            var sum = 0f;

            foreach (var stat in Helpers.List)
            {
                sum += statCollection[stat].Value;
            }

            Console.WriteLine("sum: " + sum);
            Console.WriteLine("End of summing stats: " + sw.Elapsed);

            sw.Stop();
        }