コード例 #1
0
ファイル: SeriesGenerator.cs プロジェクト: s952163/Spreads
        public static Series <DateTime, double> DummySeries(int length,
                                                            DateTime?startDate = null, TimeSpan?dateStep = null,
                                                            double startValue  = 0.0, double valueStep   = 1.0)
        {
            var sm = new SortedMap <DateTime, double>(length);

            if (startDate == null)
            {
                startDate = DateTime.Today.ToUniversalTime();
            }

            if (dateStep == null)
            {
                dateStep = TimeSpan.FromSeconds(1);
            }

            for (int i = 0; i < length; i++)
            {
                sm.AddLast(startDate.Value, startValue);
                startDate  += dateStep;
                startValue += valueStep;
            }

            return(sm);
        }
コード例 #2
0
ファイル: DateTimeExtensions.cs プロジェクト: lulzzz/Spreads
        /// <summary>
        /// Get history of offsets with keys as zoned time. Used to convert from zoned to UTC time.
        /// </summary>
        /// <returns></returns>
        public static SortedMap <DateTime, long> GetOffsetsFromZoned(string tzFrom, bool standardOffsetOnly = false)
        {
            string tz;

            if (!Normalizer.TryGetValue(tzFrom.ToLowerInvariant(), out tz))
            {
                tz = tzFrom;
            }
            var sortedMap = new SortedMap <DateTime, long>();

            if (tz.ToLowerInvariant() == "utc")
            {
                sortedMap[new DateTime(0L, DateTimeKind.Unspecified)] = 0;
            }
            else
            {
                var givenTz   = DateTimeZoneProviders.Tzdb[tz];
                var intervals = givenTz.GetZoneIntervals(Instant.FromDateTimeUtc(
                                                             // https://en.wikipedia.org/wiki/International_Meridian_Conference
                                                             new DateTime(1884, 10, 22, 12, 0, 0, DateTimeKind.Utc)
                                                             ), Instant.MaxValue);
                foreach (var interval in intervals)
                {
                    var localStart  = interval.IsoLocalStart.ToDateTimeUnspecified();
                    var offset      = standardOffsetOnly ? interval.StandardOffset : interval.WallOffset;
                    var offsetTicks = offset.Ticks;
                    sortedMap.AddLast(localStart, offsetTicks);
                }
            }
            sortedMap.Complete();
            return(sortedMap);
        }
コード例 #3
0
ファイル: SeriesStorage.cs プロジェクト: s952163/Spreads
 internal virtual Task<SortedMap<long, SeriesChunk>> LoadKeys(long mapid, long version) {
     var sm = new SortedMap<long, SeriesChunk>();
     var chunks = _connection.Query<SeriesChunk>("SELECT Id, ChunkKey, Count, Version from " + ChunkTableName + "" + " WHERE Id = @Id ORDER BY ChunkKey", new { Id = mapid });
     foreach (var ch in chunks) {
         sm.AddLast(ch.ChunkKey, ch);
     }
     return Task.FromResult(sm);
 }
コード例 #4
0
ファイル: ArithmeticTests.cs プロジェクト: mindis/Spreads
        public void CouldMapValuesWithOperatorBenchmark()
        {
            var sm    = new SortedMap <int, double>();
            var count = 10000000;

            sm.AddLast(0, 0);
            for (int i = 2; i < count; i++)
            {
                sm.AddLast(i, i);
            }

            for (int r = 0; r < 10; r++)
            {
                var sw = new Stopwatch();
                sw.Restart();
                var map  = (sm as BaseSeries <int, double, ICursor <int, double> >) * 2;
                var map2 = map * 2;
                var sum  = 0.0;
                foreach (var kvp in map2)
                {
                    sum += kvp.Value;
                }
                sw.Stop();
                Assert.IsTrue(sum > 0);

                Console.WriteLine($"Mops {sw.MOPS(count)}");
            }

            for (int r = 0; r < 10; r++)
            {
                var sw = new Stopwatch();
                sw.Restart();
                var map = (sm as BaseSeries <int, double, ICursor <int, double> >)
                          .Select(x => new KeyValuePair <int, double>(x.Key, x.Value * 2))
                          .Select(x => new KeyValuePair <int, double>(x.Key, x.Value * 2));
                var sum = 0.0;
                foreach (var kvp in map)
                {
                    sum += kvp.Value;
                }
                sw.Stop();
                Assert.IsTrue(sum > 0);

                Console.WriteLine($"LINQ Mops {sw.MOPS(count)}");
            }
        }
コード例 #5
0
ファイル: MapCursorTest.cs プロジェクト: lulzzz/Spreads
        public void CouldMapValuesBenchmark()
        {
            var sm    = new SortedMap <int, double>();
            var count = 10000000;

            sm.AddLast(0, 0);
            for (int i = 2; i < count; i++)
            {
                sm.AddLast(i, i);
            }

            for (int r = 0; r < 10; r++)
            {
                var sw = new Stopwatch();
                sw.Restart();
                var map  = new MapValuesSeries <int, double, double, SortedMapCursor <int, double> >(sm.GetEnumerator(), i => i * 2);
                var map2 = new MapValuesSeries <int, double, double, MapValuesSeries <int, double, double, SortedMapCursor <int, double> > >(map, i => i * 2);
                var sum  = 0.0;
                foreach (var kvp in map2)
                {
                    sum += kvp.Value;
                }
                sw.Stop();
                Assert.IsTrue(sum > 0);

                Console.WriteLine($"Mops {sw.MOPS(count)}");
            }

            //for (int r = 0; r < 10; r++)
            //{
            //    var sw = new Stopwatch();
            //    sw.Restart();
            //    var map = sm
            //        //.Select(x => new KeyValuePair<int, double>(x.Key, x.Value * 2))
            //        .Select(x => new KeyValuePair<int, double>(x.Key, x.Value * 2));
            //    var sum = 0.0;
            //    foreach (var kvp in map)
            //    {
            //        sum += kvp.Value;
            //    }
            //    sw.Stop();
            //    Assert.IsTrue(sum > 0);

            //    Console.WriteLine($"LINQ Mops {sw.MOPS(count)}");
            //}
        }
コード例 #6
0
ファイル: ArithmeticTests.cs プロジェクト: lulzzz/Spreads
        public void CouldMapValuesWithOperatorBenchmark()
        {
            var sm    = new SortedMap <int, double>();
            var count = 10000000;

            sm.AddLast(0, 0);
            for (int i = 2; i < count; i++)
            {
                sm.AddLast(i, i);
            }

            for (int r = 0; r < 10; r++)
            {
                var map  = sm * 2;
                var map2 = map * 2;
                var sum  = 0.0;
                using (Benchmark.Run("BaseSeries", count))
                {
                    foreach (var kvp in map2)
                    {
                        sum += kvp.Value;
                    }
                }
                Assert.IsTrue(sum > 0);
            }

            for (int r = 0; r < 10; r++)
            {
                var map = sm
                          .Select(x => new KeyValuePair <int, double>(x.Key, x.Value * 2))
                          .Select(x => new KeyValuePair <int, double>(x.Key, x.Value * 2));
                var sum = 0.0;
                using (Benchmark.Run("LINQ", count))
                {
                    foreach (var kvp in map)
                    {
                        sum += kvp.Value;
                    }
                }
                Assert.IsTrue(sum > 0);
            }
        }
コード例 #7
0
        public static SortedMap <TKey, TValue> ToSortedMap <TKey, TValue>(
            this IDataStream <TKey, TValue> dataStream)
        {
            var sm = new SortedMap <TKey, TValue>(dataStream.Comparer);

            foreach (var keyValuePair in dataStream)
            {
                sm.AddLast(keyValuePair.Key, keyValuePair.Value);
            }

            return(sm);
        }
コード例 #8
0
ファイル: MapValuesTest.cs プロジェクト: bryant1410/Spreads
        public void CouldMapRangeSeriesViaExtensionMethodsBenchmark()
        {
            var sm    = new SortedMap <int, int>();
            var count = 10000000;

            for (int i = 0; i < count; i++)
            {
                sm.AddLast(i, i);
            }

            for (int r = 0; r < 10; r++)
            {
                var sw = new Stopwatch();
                sw.Restart();
                var range = sm.Range(new Opt <int>(0), Opt <int> .Missing, true, true);
                var map   = sm.Map(i => i * 2);
                //var range2 = map.Range(0, int.MaxValue, true, true);
                //var map2 = range2.Map(i => i * 2);
                //var range3 = map2.Range(0, int.MaxValue, true, true);
                //var map3 = range3.Map(i => i * 2);
                long sum = 0;
                foreach (var kvp in map)
                {
                    sum += kvp.Value;
                }
                sw.Stop();
                Assert.IsTrue(sum > 0);

                Console.WriteLine($"Mops {sw.MOPS(count)}");
            }

            //for (int r = 0; r < 10; r++)
            //{
            //    var sw = new Stopwatch();
            //    sw.Restart();
            //    var map = sm.Select(x => new KeyValuePair<int, int>(x.Key, x.Value * 2));
            //    long sum = 0;
            //    foreach (var kvp in map)
            //    {
            //        sum += kvp.Value;
            //    }
            //    sw.Stop();
            //    Assert.IsTrue(sum > 0);

            //    Console.WriteLine($"LINQ Mops {sw.MOPS(count)}");
            //}
        }
コード例 #9
0
ファイル: ArithmeticTests.cs プロジェクト: lulzzz/Spreads
        public void CouldMapValuesBenchmark()
        {
            var sm    = new SortedMap <int, double>();
            var count = 10000000;

            //sm.AddLast(0, 0); // make irregular
            for (int i = 2; i < count; i++)
            {
                sm.AddLast(i, i);
            }

            for (int r = 0; r < 10; r++)
            {
                var map =
                    new ArithmeticSeries <int, double, MultiplyOp <double>, SortedMapCursor <int, double> >(
                        sm.GetEnumerator(), 2.0);
                var map2 =
                    new ArithmeticSeries <int, double, MultiplyOp <double>, ArithmeticSeries <int, double,
                                                                                              MultiplyOp <double>, SortedMapCursor <int, double> > >(
                        map, 2.0);
                var sum = 0.0;
                using (Benchmark.Run("ArithmeticSeries", count))
                {
                    foreach (var kvp in map2)
                    {
                        sum += kvp.Value;
                    }
                }
                Assert.IsTrue(sum > 0);
            }

            for (int r = 0; r < 10; r++)
            {
                var map = sm
                          //.Select(x => new KeyValuePair<int, double>(x.Key, x.Value * 2))
                          .Select(x => new KeyValuePair <int, double>(x.Key, x.Value * 2));
                var sum = 0.0;
                using (Benchmark.Run("LINQ", count))
                {
                    foreach (var kvp in map)
                    {
                        sum += kvp.Value;
                    }
                }
                Assert.IsTrue(sum > 0);
            }
        }
コード例 #10
0
ファイル: MapValuesTest.cs プロジェクト: bryant1410/Spreads
        public void CouldMapValuesViaExtensionMethodsBenchmark()
        {
            var sm    = new SortedMap <int, int>();
            var count = 10000000;

            for (int i = 0; i < count; i++)
            {
                sm.AddLast(i, i);
            }

            for (int r = 0; r < 10; r++)
            {
                var sw = new Stopwatch();
                sw.Restart();
                var  map = sm.Map(i => i * 2).Range(0, int.MaxValue, true, true).Map(i => i * 2).Map(i => i * 2);
                long sum = 0;
                foreach (var kvp in map)
                {
                    sum += kvp.Value;
                }
                sw.Stop();
                Assert.IsTrue(sum > 0);

                Console.WriteLine($"Mops {sw.MOPS(count)}");
            }

            for (int r = 0; r < 10; r++)
            {
                var sw = new Stopwatch();
                sw.Restart();
                var map = sm
                          .Select(x => new KeyValuePair <int, int>(x.Key, x.Value * 2))
                          .Select(x => new KeyValuePair <int, int>(x.Key, x.Value * 2))
                          .Select(x => new KeyValuePair <int, int>(x.Key, x.Value * 2));
                long sum = 0;
                foreach (var kvp in map)
                {
                    sum += kvp.Value;
                }
                sw.Stop();
                Assert.IsTrue(sum > 0);

                Console.WriteLine($"LINQ Mops {sw.MOPS(count)}");
            }
        }
コード例 #11
0
ファイル: MapValuesTest.cs プロジェクト: bryant1410/Spreads
        public void CouldMapValuesBenchmark()
        {
            var sm    = new SortedMap <int, int>();
            var count = 10000000;

            for (int i = 0; i < count; i++)
            {
                sm.AddLast(i, i);
            }

            for (int r = 0; r < 10; r++)
            {
                var sw = new Stopwatch();
                sw.Restart();
                var  map = new MapValuesSeries <int, int, int, SortedMapCursor <int, int> >(sm, i => i * 2);
                long sum = 0;
                foreach (var kvp in map)
                {
                    sum += kvp.Value;
                }
                sw.Stop();
                Assert.IsTrue(sum > 0);

                Console.WriteLine($"Mops {sw.MOPS(count)}");
            }

            for (int r = 0; r < 10; r++)
            {
                var sw = new Stopwatch();
                sw.Restart();
                var  map = sm.Select(x => new KeyValuePair <int, int>(x.Key, x.Value * 2));
                long sum = 0;
                foreach (var kvp in map)
                {
                    sum += kvp.Value;
                }
                sw.Stop();
                Assert.IsTrue(sum > 0);

                Console.WriteLine($"LINQ Mops {sw.MOPS(count)}");
            }
        }
コード例 #12
0
ファイル: ZipNTests.cs プロジェクト: b-e-n-j/Spreads
        public void ContinuousZipIsCorrectByRandomCheck() {
            var sw = new Stopwatch();

            var sm1 = new SortedMap<int, int>();
            var sm2 = new SortedMap<int, int>();


            var rng = new System.Random(31415926); //31415926

            var prev1 = 0;
            var prev2 = 0;

            for (int i = 0; i < 100000; i = i + 1) {
                prev1 = prev1 + rng.Next(1, 11);
                sm1.Add(prev1, prev1);
                prev2 = prev2 + rng.Next(1, 11);
                sm2.Add(prev2, prev2);
            }
            sm1.IsMutable = false;
            sm2.IsMutable = false;

            //Console.WriteLine("First map:");
            //foreach (var kvp in sm1)
            //{
            //    Console.WriteLine(kvp.Key);
            //}

            //Console.WriteLine("Second map:");
            //foreach (var kvp in sm2) {
            //    Console.WriteLine(kvp.Key);
            //}

            var series = new[] { sm1.Repeat(), sm2.Repeat(), };

            sw.Start();
            var allKeys = sm1.keys.Union(sm2.keys).OrderBy(x => x).ToArray();
            int[] expectedKeys = new int[allKeys.Length];
            int[] expectedValues = new int[allKeys.Length];
            var size = 0;
            for (int i = 0; i < allKeys.Length; i++) {
                var val = 0;
                KeyValuePair<int, int> temp;
                var hasFirst = sm1.TryFind(allKeys[i], Lookup.LE, out temp);
                if (hasFirst) {
                    val += temp.Value;
                    var hasSecond = sm2.TryFind(allKeys[i], Lookup.LE, out temp);
                    if (hasSecond) {
                        val += temp.Value;
                        expectedKeys[size] = allKeys[i];
                        expectedValues[size] = val;
                        size++;
                    }
                }
            }

            var expectedMap = SortedMap<int, int>.OfSortedKeysAndValues(expectedKeys, expectedValues, size);

            sw.Stop();

            //Console.WriteLine("Expected map:");
            //foreach (var kvp in expectedMap) {
            //    Console.WriteLine(kvp.Key + " ; " + kvp.Value);
            //}

            Console.WriteLine("Manual join, elapsed msec: {0}", sw.ElapsedMilliseconds);


            SortedMap<int, int> sum = new SortedMap<int, int>();

            for (int round = 0; round < 1; round++) {
                sw.Restart();
                var ser = series.Zip((k, varr) => varr.Sum());

                var cur = ser.GetCursor();
                while (cur.MoveNext()) {
                    sum.AddLast(cur.CurrentKey, cur.CurrentValue);
                }

                sw.Stop();
                Console.WriteLine("Zip join, elapsed msec: {0}", sw.ElapsedMilliseconds);
                //Console.WriteLine("StateCreation: {0}", RepeatCursor<int, int>.StateCreation);
                //Console.WriteLine("StateHit: {0}", RepeatCursor<int, int>.StateHit);
                //Console.WriteLine("StateMiss: {0}", RepeatCursor<int, int>.StateMiss);
            }



            //Console.WriteLine("Sync zip map:");
            //foreach (var kvp in sum) {
            //    Console.WriteLine(kvp.Key + " ; " + kvp.Value);
            //}
            Assert.AreEqual(expectedMap.Count, sum.Count, "Results of sync and expected must be equal");

            foreach (var kvp in expectedMap) {
                Assert.AreEqual(kvp.Value, sum[kvp.Key]);
            }

            for (int round = 0; round < 1; round++) {
                sw.Restart();
                var ser = series.Zip((k, varr) => varr.Sum());

                var cur = ser.GetCursor();

                var cur2 = cur.Clone();
                var sum2 = new SortedMap<int, int>();
                Task.Run(async () => {
                    while (await cur2.MoveNext(CancellationToken.None)) {
                        sum2.Add(cur2.CurrentKey, cur2.CurrentValue);
                    }
                }).Wait();

                sw.Stop();
                Console.WriteLine("Async Zip join, elapsed msec: {0}", sw.ElapsedMilliseconds);
                Assert.AreEqual(sum.Count, sum2.Count, "Results of sync and async moves must be equal");
                foreach (var kvp in expectedMap) {
                    Assert.AreEqual(kvp.Value, sum2[kvp.Key]);
                }


            }

            Console.WriteLine("");
        }
コード例 #13
0
ファイル: ArithmeticTests.cs プロジェクト: mindis/Spreads
        public void CouldMapValuesBenchmarkArithmeticVsMapCursor()
        {
            var sm    = new SortedMap <int, double>();
            var count = 10000000;

            sm.AddLast(0, 0);
            for (int i = 2; i < count; i++)
            {
                sm.AddLast(i, i);
            }

            for (int r = 0; r < 10; r++)
            {
                var sw = new Stopwatch();

                {
                    sw.Restart();

                    var sum = 0.0;
                    foreach (var kvp in sm)
                    {
                        sum += kvp.Value;
                    }
                    sw.Stop();
                    Assert.IsTrue(sum > 0);
                    Console.WriteLine($"SortedMap {sw.MOPS(count)}");
                }

                {
                    sw.Restart();
                    var map =
                        new ArithmeticSeries <int, double, MultiplyOp <double>, SortedMapCursor <int, double> >(sm, 2.0);
                    var map2 =
                        new ArithmeticSeries <int, double, MultiplyOp <double>, ArithmeticSeries <int, double, MultiplyOp <double>, SortedMapCursor <int, double> > >(
                            map, 2.0);

                    var sum = 0.0;
                    foreach (var kvp in map2)
                    {
                        sum += kvp.Value;
                    }
                    sw.Stop();
                    Assert.IsTrue(sum > 0);
                    Console.WriteLine($"ArithmeticSeries {sw.MOPS(count)}");
                }

                {
                    sw.Restart();
                    var map  = new MapValuesSeries <int, double, double, SortedMapCursor <int, double> >(sm, i => Apply(i, 2.0));
                    var map2 = new MapValuesSeries <int, double, double, MapValuesSeries <int, double, double, SortedMapCursor <int, double> > >(map, i => Apply(i, 2.0));
                    var sum  = 0.0;
                    foreach (var kvp in map2)
                    {
                        sum += kvp.Value;
                    }
                    sw.Stop();
                    Assert.IsTrue(sum > 0);

                    Console.WriteLine($"MapValuesSeries {sw.MOPS(count)}");
                }

                {
                    sw.Restart();
                    var map = sm
                              .Select(x => new KeyValuePair <int, double>(x.Key, x.Value * 2))
                              .Select(x => new KeyValuePair <int, double>(x.Key, x.Value * 2));
                    var sum = 0.0;
                    foreach (var kvp in map)
                    {
                        sum += kvp.Value;
                    }
                    sw.Stop();
                    Assert.IsTrue(sum > 0);

                    Console.WriteLine($"LINQ {sw.MOPS(count)}");
                }
            }
        }
コード例 #14
0
ファイル: ArithmeticTests.cs プロジェクト: lulzzz/Spreads
        public void CouldMapValuesBenchmarkArithmeticVsMapCursor()
        {
            var sm    = new SortedMap <int, double>();
            var count = 10000000;

            sm.AddLast(0, 0);
            for (int i = 2; i < count; i++)
            {
                sm.AddLast(i, i);
            }

            for (int r = 0; r < 10; r++)
            {
                {
                    var sum = 0.0;
                    using (Benchmark.Run("SortedMap", count))
                    {
                        foreach (var kvp in sm)
                        {
                            sum += kvp.Value;
                        }
                    }
                    Assert.IsTrue(sum > 0);
                }

                {
                    var map =
                        new ArithmeticSeries <int, double, MultiplyOp <double>, SortedMapCursor <int, double> >(sm.GetEnumerator(),
                                                                                                                2.0);
                    var map2 = map + 2;
                    //new ArithmeticSeries<int, double, MultiplyOp<double>, ArithmeticSeries<int, double,
                    //    MultiplyOp<double>, SortedMapCursor<int, double>>>(
                    //    map.Initialize, 2.0);
                    var sum = 0.0;
                    using (Benchmark.Run("ArithmeticSeries", count))
                    {
                        foreach (var kvp in map2)
                        {
                            sum += kvp.Value;
                        }
                    }
                    Assert.IsTrue(sum > 0);
                }

                {
                    var c =
                        new Op <int, double, MultiplyOp <double>, SortedMapCursor <int, double> >(
                            sm.GetEnumerator(), 2.0);
                    var c1 =
                        new Op <int, double, AddOp <double>, Op <int, double,
                                                                 MultiplyOp <double>, SortedMapCursor <int, double> > >(
                            c, 2.0);
                    var series = new Series <int, double, Op <int, double, AddOp <double>, Op <int, double,
                                                                                               MultiplyOp <double>, SortedMapCursor <int, double> > > >(c1);
                    var sum = 0.0;
                    using (Benchmark.Run("ArithmeticCursor", count))
                    {
                        foreach (var kvp in series)
                        {
                            sum += kvp.Value;
                        }
                    }
                    Assert.IsTrue(sum > 0);
                }

                {
                    var map =
                        new MapValuesSeries <int, double, double, SortedMapCursor <int, double> >(sm.GetEnumerator(),
                                                                                                  i => Apply(i, 2.0));
                    var map2 =
                        new
                        MapValuesSeries <int, double, double, MapValuesSeries <int, double, double,
                                                                               SortedMapCursor <int, double> > >(map, i => Apply2(i, 2.0));
                    var sum = 0.0;
                    using (Benchmark.Run("MapValuesSeries", count))
                    {
                        foreach (var kvp in map2)
                        {
                            sum += kvp.Value;
                        }
                    }
                    Assert.IsTrue(sum > 0);
                }

                {
                    var map = (sm * 2) + 2;
                    var sum = 0.0;
                    using (Benchmark.Run("BaseSeries operator", count))
                    {
                        foreach (var kvp in map)
                        {
                            sum += kvp.Value;
                        }
                    }
                    Assert.IsTrue(sum > 0);
                }

                {
                    var map = sm
                              .Select(x => new KeyValuePair <int, double>(x.Key, x.Value * 2))
                              .Select(x => new KeyValuePair <int, double>(x.Key, x.Value + 2));
                    var sum = 0.0;
                    using (Benchmark.Run("LINQ", count))
                    {
                        foreach (var kvp in map)
                        {
                            sum += kvp.Value;
                        }
                    }
                    Assert.IsTrue(sum > 0);
                }
            }

            Benchmark.Dump();
        }
コード例 #15
0
ファイル: ArithmeticTests.cs プロジェクト: lulzzz/Spreads
        // TODO learn how to use dotMemory for total allocatoins count
        //[DotMemoryUnit(FailIfRunWithoutSupport = false)]
        public void MultipleEnumerationDoesntAllocate()
        {
            var sm    = new SortedMap <int, double>();
            var count = 100;

            sm.AddLast(0, 0);
            for (int i = 2; i < count; i++)
            {
                sm.AddLast(i, i);
            }

            for (int r = 0; r < 10; r++)
            {
                var map =
                    new ArithmeticSeries <int, double, MultiplyOp <double>, SortedMapCursor <int, double> >(
                        sm.GetEnumerator(), 2.0);

                var sum        = 0.0;
                var sum1       = 0.0;
                var sum2       = 0.0;
                var iterations = 100000;

                void Run(ref double s)
                {
                    try
                    {
                        // using it here completely eliminates allocations, an instance is created for
                        // all iterations inside each thread
                        //using (var mapX = map + 2)
                        {
                            for (int i = 0; i < iterations; i++)
                            {
                                // here static caching helps, but not completely eliminates allocations because
                                // two threads compete for a single static slot very often
                                using (var mapX = map + 2)
                                    using (var c = (mapX).GetEnumerator())
                                    {
                                        //Assert.IsTrue(c.State == CursorState.Initialized);
                                        //Assert.IsTrue(c._cursor.State == CursorState.Initialized);

                                        var countCheck = 0;
                                        while (c.MoveNext())
                                        {
                                            s += c.CurrentKey;
                                            countCheck++;
                                        }
                                        if (sm.Count != countCheck)
                                        {
                                            Console.WriteLine($"Expected {sm.Count} vs actual {countCheck}");
                                        }
                                        if (sm.Count != countCheck)
                                        {
                                            Assert.Fail();
                                        }
                                    }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + Environment.NewLine + ex);
                    }
                }

                var cc =
                    new Op <int, double, MultiplyOp <double>, SortedMapCursor <int, double> >(
                        sm.GetEnumerator(), 2.0);

                var cc1 =
                    new Op <int, double, AddOp <double>, Op <int, double,
                                                             MultiplyOp <double>, SortedMapCursor <int, double> > >(
                        cc, 2.0);
                var series = new Series <int, double, Op <int, double, AddOp <double>, Op <int, double,
                                                                                           MultiplyOp <double>, SortedMapCursor <int, double> > > >(cc1);

                void Run2(ref double s)
                {
                    try
                    {
                        for (int i = 0; i < iterations; i++)
                        {
                            using (var c = series.GetEnumerator())
                            {
                                var countCheck = 0;
                                while (c.MoveNext())
                                {
                                    s += c.CurrentKey;
                                    countCheck++;
                                }
                                if (sm.Count != countCheck)
                                {
                                    Console.WriteLine($"Expected {sm.Count} vs actual {countCheck}");
                                }
                                if (sm.Count != countCheck)
                                {
                                    Assert.Fail();
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + Environment.NewLine + ex);
                    }
                }

                using (Benchmark.Run("ArithmeticSeries", count * iterations))
                {
                    var t  = Task.Run(() => Run(ref sum1));
                    var t1 = Task.Run(() => Run(ref sum2));
                    Run(ref sum);
                    t.Wait();
                    t1.Wait();

                    //dotMemory.Check(memory =>
                    //{
                    //    Assert.That(
                    //        memory.GetObjects(where =>
                    //            where.Type.Is<ArithmeticSeries<int, double, MultiplyOp<double>, SortedMapCursor<int, double>>>()).ObjectsCount,
                    //        Is.EqualTo(1)
                    //    );
                    //});
                }

                Assert.IsTrue(sum > 0);
                Assert.AreEqual(sum, sum1);
                Assert.AreEqual(sum, sum2);

                using (Benchmark.Run("ArithmeticCursor", count * iterations))
                {
                    var t  = Task.Run(() => Run2(ref sum1));
                    var t1 = Task.Run(() => Run2(ref sum2));
                    Run2(ref sum);
                    t.Wait();
                    t1.Wait();
                }

                Assert.IsTrue(sum > 0);
                Assert.AreEqual(sum, sum1);
                Assert.AreEqual(sum, sum2);
            }
            Benchmark.Dump("Compare multiple allocations and subsequent enumerations of arithmetic series.");
        }
コード例 #16
0
ファイル: ArithmeticTests.cs プロジェクト: lulzzz/Spreads
        public void CouldUseStructSeries()
        {
            var sm    = new SortedMap <int, double>();
            var count = 10000000;

            sm.AddLast(0, 0); // make irregular
            for (int i = 2; i < count; i++)
            {
                sm.AddLast(i, i);
            }

            for (int r = 0; r < 10; r++)
            {
                var sum = 0.0;
                {
                    using (Benchmark.Run("SortedMap", count))
                    {
                        foreach (var kvp in sm)
                        {
                            sum += kvp.Value;
                        }
                    }
                    Assert.IsTrue(sum > 0);
                }

                sum = 0.0;
                {
                    var map =
                        new ArithmeticSeries <int, double, MultiplyOp <double>, SortedMapCursor <int, double> >(
                            sm.GetEnumerator(), 2.0);
                    var map2 =
                        new ArithmeticSeries <int, double, AddOp <double>, ArithmeticSeries <int, double,
                                                                                             MultiplyOp <double>, SortedMapCursor <int, double> > >(
                            map, 2.0);

                    using (Benchmark.Run("ArithmeticSeries", count))
                    {
                        foreach (var kvp in map2)
                        {
                            sum += kvp.Value;
                        }
                    }
                    Assert.IsTrue(sum > 0);
                }
                var sum1 = 0.0;
                {
                    var c =
                        new Op <int, double, MultiplyOp <double>, SortedMapCursor <int, double> >(
                            sm.GetEnumerator(), 2.0);
                    var c1 =
                        new Op <int, double, AddOp <double>, Op <int, double,
                                                                 MultiplyOp <double>, SortedMapCursor <int, double> > >(
                            c, 2.0);

                    using (Benchmark.Run("ArithmeticCursor", count))
                    {
                        foreach (var kvp in c1.Source)
                        {
                            sum1 += kvp.Value;
                        }
                    }
                    Assert.IsTrue(sum1 > 0);
                }

                Assert.AreEqual(sum, sum1);
            }

            Benchmark.Dump("Compare enumeration speed of SortedMap and two arithmetic implementations using class and struct (item workload is multiply by 2 then add 2).");
        }