I’ve got a class that have 3 methods
- public DoSth
- private DoItForReal
- private DoSthExtra
User can call one of 2 constructors (object can be changed olny during construction)
I want user to call DoSth method and depending on with constructor was called I wand to call:
– DoItForReal (only!)
– DoItForReal and DoSthExtra
My question is wchih implementation is better of performance (DoSth will be called 30 times a sec) ?
Implementation1 (using if statemant):
DoSth()
{
DoItForReal();
if (ctor1)
DoSthExtra();
}
Implementation2 (using events):
private event Action DoSthEvent;
Ctor1()
{
DoSthEvent += DoItForReal;
DoSthEvent += DoSthExtra;
}
Ctor2()
{
DoSthEvent += DoItForReal;
}
DoSth()
{
DoSthEvent();
}
30 times per second is nothing. You should definitely go with the first solution – it’s far clearer what’s going on. (Why you’d want to use an event for this even in the second case is beyond me… A simple
Actiontype variable would be better. It’s not like you’re really using this like an event.)I suspect the first form is faster as well, but it’s not even worth benchmarking – because any difference in speed will be entirely negligible when you’re talking about calling it 30 times per second.