IgnoreElementsBenchmark.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using BenchmarkDotNet.Attributes;
  9. namespace Benchmarks.System.Interactive
  10. {
  11. [MemoryDiagnoser]
  12. public class IgnoreElementsBenchmark
  13. {
  14. [Params(1, 10, 100, 1000, 10000, 100000, 1000000)]
  15. public int N;
  16. private int _store;
  17. private int[] _array;
  18. private List<int> _list;
  19. [Benchmark]
  20. public void Ignore()
  21. {
  22. Enumerable.Range(1, N)
  23. .IgnoreElements()
  24. .Subscribe(v => Volatile.Write(ref _store, v));
  25. }
  26. [Benchmark]
  27. public void IgnoreList()
  28. {
  29. _list
  30. .IgnoreElements()
  31. .Subscribe(v => Volatile.Write(ref _store, v));
  32. }
  33. [Benchmark]
  34. public void IgnoreArray()
  35. {
  36. _array
  37. .IgnoreElements()
  38. .Subscribe(v => Volatile.Write(ref _store, v));
  39. }
  40. [GlobalSetup]
  41. public void Setup()
  42. {
  43. _array = new int[N];
  44. _list = new List<int>(N);
  45. for (var i = 0; i < N; i++)
  46. {
  47. _array[i] = i;
  48. _list.Add(i);
  49. }
  50. }
  51. }
  52. }