IgnoreElementsBenchmark.cs 1.4 KB

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