Given the following RX code:
static void Main(string[] args)
{
int x = 0;
const int timerMilliseconds = 1000 * 30; //30 seconds
var disposable = Observable.Generate(0, i => true, i => 1, i => 1, i => TimeSpan.FromMilliseconds(1))
.Subscribe(i => Interlocked.Increment(ref x));
var timer = new Timer(o =>
{
disposable.Dispose();
Console.WriteLine("Disposed of observable. Current total: " + x);
}, null, timerMilliseconds, timerMilliseconds);
Console.ReadKey(true);
timer.Dispose();
}
If I run this code, the output after 30 seconds (on my machine) is ~1924, which is kind of surprising to me. I would have expected that with a delay of one millisecond, after 30 seconds the number should be closer to ~30,000. It must be something obvious, but what am I missing here?
I think you are forgetting that the Windows operating system doesn’t give you a hard guarantee that the generating and/or observing thread will be scheduled to run within the next millisecond. Depending on the state of your system some overhead wrt to context switching might be involved as well. On my rusty old laptop I didn’t manage to get below ~20 ms. (I measured the time between invocations of the “Subscribe” lambda with the help of a
Stopwatch.)