2

Which one would be more efficient, and why? I'm just getting started with RX and trying to understand how to write better code.

For example I could do

tradesfeed.Where(trade=>trade.Size > 500).Subscribe(x =>
{
    Console.WriteLine("big trade: " + x.Symbol + " " + x.Size);
});

tradesfeed.Where(trade=>trade.Size <= 500).Subscribe(x =>
{
    Console.WriteLine("little trade: " + x.Symbol + " " + x.Size);
});

or have only one subscription

tradesfeed.Subscribe(x =>
{
    if (x.Size > 500)
        Console.WriteLine("big trade: " + x.Symbol + " " + x.Size);
    else
        Console.WriteLine("little trade: " + x.Symbol + " " + x.Size);
});
Lasse Espeholt
  • 17,622
  • 5
  • 63
  • 99
b4m
  • 21
  • 1
  • 5
    Lets assume the difference is less than 1%. Should performance then be the first consideration how you write your code? –  Aug 25 '10 at 20:58
  • Second is more efficient (negligible) but harder to re-factor. I use first 95% of the time. – Sergey Aldoukhov Aug 25 '10 at 22:14

1 Answers1

2

The second is more efficient in terms of fewer delegate allocations. But the difference would be so minute, it should not at all be considered a factor in your choice. Go with whatever is simpler for your code and don't worry about micro-optimizations.

Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212