PrependVsStartWtihBenchmark.cs 1.9 KB

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