Пример #1
0
        public void WritePerformanceDirect()
        {
            const int width  = 150;
            const int height = 150;

            var grid          = new int[width, height];
            var immutableGrid = ImmutableGrid <int> .Create(width, height);

            var gridTime = BenchmarkHelper(
                width, height, (x, y) => grid[x, y] = x + y);

            var immutableGridTime = BenchmarkHelper(
                width, height, (x, y) => immutableGrid.Set(x, y, x + y));

            // Use the builder for many updates :)
            // This looks ugly but still 1/2 time of nested immutable
            // dictionary
            var factor = immutableGridTime / gridTime;

#if DEBUG
            Assert.IsTrue(factor < 800, "Factor was " + factor);
#else
            Assert.IsTrue(factor < 700, "Factor was " + factor);
#endif
        }
Пример #2
0
        public void CreateReferenceType()
        {
            var grid = ImmutableGrid <object> .Create(10, 20);

            Assert.AreEqual(10, grid.Width);
            Assert.AreEqual(20, grid.Height);
            Assert.IsTrue(grid.All(x => x.Item2 == default(object)));
        }
Пример #3
0
        public void CreateValueType()
        {
            var grid = ImmutableGrid <int> .Create(10, 20);

            Assert.AreEqual(10, grid.Width);
            Assert.AreEqual(20, grid.Height);
            Assert.IsTrue(grid.All(x => x.Item2 == default(int)));
        }
Пример #4
0
        public void Set()
        {
            var source = ImmutableGrid <int> .Create(2, 2).Set(0, 0, 5);

            var grid = source.Set(0, 0, 6);

            Assert.AreEqual(6, grid[0, 0]);

            Assert.AreEqual(5, source[0, 0]);
        }
Пример #5
0
        public void Indexer()
        {
            var grid = ImmutableGrid <int>
                       .Create(2, 2)
                       .Set(0, 0, 5)
                       .Set(0, 1, 6);

            Assert.AreEqual(5, grid[0, 0]);
            Assert.AreEqual(6, grid[0, 1]);
        }
Пример #6
0
        public void ToBuilder()
        {
            var source = ImmutableGrid <int> .Create(2, 2).Set(0, 0, 5);

            var grid = source.ToBuilder().Set(1, 1, 3).Build();

            Assert.AreEqual(5, grid[0, 0]);
            Assert.AreEqual(3, grid[1, 1]);

            Assert.AreEqual(5, source[0, 0]);
            Assert.AreEqual(default(int), source[1, 1]);
        }
Пример #7
0
        public void ReadPerformance()
        {
            const int width  = 150;
            const int height = 150;

            var grid          = new int[width, height];
            var immutableGrid = ImmutableGrid <int> .Create(width, height);

            var gridTime = BenchmarkHelper(
                width, height, (x, y) => { var cell = grid[x, y]; });

            var immutableGridTime = BenchmarkHelper(
                width, height, (x, y) => { var cell = immutableGrid[x, y]; });

            var factor = immutableGridTime / gridTime;

#if DEBUG
            Assert.IsTrue(factor < 5, "Factor was " + factor);
#else
            Assert.IsTrue(factor < 2, "Factor was " + factor);
#endif
        }