Ver código fonte

Ix: Improve IgnoreElements() performance

Dávid Karnok 7 anos atrás
pai
commit
949f2fcbea

+ 29 - 0
Ix.NET/Source/Benchmarks.System.Interactive/IgnoreElementsBenchmark.cs

@@ -0,0 +1,29 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading;
+using BenchmarkDotNet.Attributes;
+
+namespace Benchmarks.System.Interactive
+{
+    [MemoryDiagnoser]
+    public class IgnoreElementsBenchmark
+    {
+        [Params(1, 10, 100, 1000, 10000, 100000, 1000000)]
+        public int N;
+
+        private int _store;
+
+        [Benchmark]
+        public void Ignore()
+        {
+            Enumerable.Range(1, N)
+                .IgnoreElements()
+                .Subscribe(v => Volatile.Write(ref _store, v));
+        }
+    }
+}

+ 1 - 0
Ix.NET/Source/Benchmarks.System.Interactive/Program.cs

@@ -18,6 +18,7 @@ namespace Benchmarks.System.Interactive
 
             var switcher = new BenchmarkSwitcher(new[] {
                 typeof(BufferCountBenchmark),
+                typeof(IgnoreElementsBenchmark),
             });
 
             switcher.Run();

+ 4 - 5
Ix.NET/Source/System.Interactive/IgnoreElements.cs

@@ -26,12 +26,11 @@ namespace System.Linq
 
         private static IEnumerable<TSource> IgnoreElements_<TSource>(this IEnumerable<TSource> source)
         {
-            foreach (var item in source)
-            {
-                ;
-            }
+            var enumerator = source.GetEnumerator();
+
+            while (enumerator.MoveNext()) ;
 
             yield break;
         }
     }
-}
+}