1
0

PrependVsStartWtihBenchmark.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #if (CURRENT)
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reactive.Linq;
  8. using System.Threading;
  9. using BenchmarkDotNet.Attributes;
  10. namespace Benchmarks.System.Reactive
  11. {
  12. [MemoryDiagnoser]
  13. public class PrependVsStartWtihBenchmark
  14. {
  15. private int _store;
  16. #pragma warning disable IDE0052 // (Remove unread private members.) We want to store results to prevent the benchmarked code from being optimized out of existence.
  17. private IObservable<int> _obsStore;
  18. #pragma warning restore IDE0052
  19. [Benchmark(Baseline = true)]
  20. public void Prepend()
  21. {
  22. Observable
  23. .Empty<int>()
  24. .Prepend(0)
  25. .Subscribe(v => Volatile.Write(ref _store, v));
  26. }
  27. [Benchmark]
  28. public void Prepend_Create()
  29. {
  30. _obsStore = Observable
  31. .Empty<int>()
  32. .Prepend(0);
  33. }
  34. private static readonly IObservable<int> _prependObservable = Observable.Empty<int>().Prepend(0);
  35. [Benchmark]
  36. public void Prepend_Subscribe()
  37. {
  38. _prependObservable
  39. .Subscribe(v => Volatile.Write(ref _store, v));
  40. }
  41. [Benchmark]
  42. public void StartWith()
  43. {
  44. Observable
  45. .Empty<int>()
  46. .StartWith(0)
  47. .Subscribe(v => Volatile.Write(ref _store, v));
  48. }
  49. [Benchmark]
  50. public void StartWith_Create()
  51. {
  52. _obsStore = Observable
  53. .Empty<int>()
  54. .StartWith(0);
  55. }
  56. private static readonly IObservable<int> _startWithObservable = Observable.Empty<int>().StartWith(0);
  57. [Benchmark]
  58. public void StartWith_Subscribe()
  59. {
  60. _startWithObservable
  61. .Subscribe(v => Volatile.Write(ref _store, v));
  62. }
  63. }
  64. }
  65. #endif