Why is IEnumerable.ToObservable so slow?
This is the difference between a well behaved observable and a "roll-your-own-because-you-think-faster-is-better-but-it-is-not" observable.
When you dive down far enough in the source you discover this lovely little line:
scheduler.Schedule(this, (IScheduler innerScheduler, _ @this) => @this.LoopRec(innerScheduler));
The is effectively calling hasNext = enumerator.MoveNext();
once per scheduled recursive iteration.
This allows you to choose the scheduler for your .ToObservable(schedulerOfYourChoice)
call.
With the other options you've chosen you've created a bare-to-the-bone series of calls to .OnNext
that virtually do nothing. Method2
doesn't even has a .Subscribe
call.
Both of Method2
and Method1
run using the current thread and both run to completion before the subscription is finished. They are blocking calls. They can cause race conditions.
Method1
is the only one that behaves nicely as an observable. It is asynchronous and it can run independently of the subscriber.
Do keep in mind that observables are collections that run over time. They typically have an async source or a timer or the respond to external stimulus. They don't often run off of a plain enumerable. If you're working with an enumerable then working synchronously should be expected to run faster.
Speed is not the goal of Rx. Performing complex queries on time-based, pushed values is the goal.