PrependVsStartWtihBenchmark.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #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. [Benchmark(Baseline = true)]
  17. public void Prepend()
  18. {
  19. Observable
  20. .Empty<int>()
  21. .Prepend(0)
  22. .Subscribe(v => Volatile.Write(ref _store, v));
  23. }
  24. [Benchmark]
  25. public void Prepend_Create()
  26. {
  27. Observable
  28. .Empty<int>()
  29. .Prepend(0);
  30. }
  31. static readonly IObservable<int> _prependObservable = Observable.Empty<int>().Prepend(0);
  32. [Benchmark]
  33. public void Prepend_Subscribe()
  34. {
  35. _prependObservable
  36. .Subscribe(v => Volatile.Write(ref _store, v));
  37. }
  38. [Benchmark]
  39. public void StartWith()
  40. {
  41. Observable
  42. .Empty<int>()
  43. .StartWith(0)
  44. .Subscribe(v => Volatile.Write(ref _store, v));
  45. }
  46. [Benchmark]
  47. public void StartWith_Create()
  48. {
  49. Observable
  50. .Empty<int>()
  51. .StartWith(0);
  52. }
  53. static readonly IObservable<int> _startWithObservable = Observable.Empty<int>().StartWith(0);
  54. [Benchmark]
  55. public void StartWith_Subscribe()
  56. {
  57. _startWithObservable
  58. .Subscribe(v => Volatile.Write(ref _store, v));
  59. }
  60. }
  61. }
  62. #endif