2

Im trying to make my code faster and i got a lot of If-else and if-or in it. I know that switch case is faster if you got more than 5 if/case. So how fast is if-else vs. if-or, is they the same?

if (item.Datum.Substring(5, 5) == "06-20" || item.Datum.Substring(5, 5) == "06-21")
{
  Something
}
else if item.Datum.Substring(5, 5) == "06-22" || item.Datum.Substring(5, 5) == "06-23")
{
  Something
}

OR

if (item.Datum.Substring(5, 5) == "06-20") 
{
  Something
}
else if (item.Datum.Substring(5, 5) == "06-21")
{
  Something
}
else if (item.Datum.Substring(5, 5) == "06-22")
{
  Something
}
else if (item.Datum.Substring(5, 5) == "06-23")
{
  Something
}

OR shall i just go with the switch case?

switch(item.Datum.Substring(5, 5))
{
   case "06-20", "06,21":
      Something
      break;
   case "06-22", "06,23":
      Something
      break;
}
pavel
  • 26,538
  • 10
  • 45
  • 61
Sefan
  • 699
  • 1
  • 8
  • 23
  • 1
    http://stackoverflow.com/questions/395618/is-there-any-significant-difference-between-using-if-else-and-switch-case-in-c – DarkBee Jul 24 '14 at 07:19
  • [Which is faster ?](http://ericlippert.com/2012/12/17/performance-rant/) – Sriram Sakthivel Jul 24 '14 at 07:19
  • 6
    Aside from anything else, don't call `Substring` so many times! – Jon Skeet Jul 24 '14 at 07:19
  • If only you don't call this function million times a day, I'd consider it premature optimization. I bet "`Something`" function will take much more time than the time you save by implementing "case" switch. – naivists Jul 24 '14 at 07:20
  • 2
    Have you profiled your code? Is this really the bottleneck in its performance? And if so, have you profiled the alternatives? If so, pick the one which you've *measured* to have the best performance. – Damien_The_Unbeliever Jul 24 '14 at 07:20
  • _Aside from anything else, don't call Substring so many times!_ @Jon: I'm curious (and also quite unable/unwilling) to learn diving into reading IL: Do all those `Substring(5,5)` __stay__ in there and get __evaluated__ all the time or are they phased out by the compiler as I always have assumed?? – TaW Jul 24 '14 at 07:27
  • use [string.CompareOrdinal(strA, indexA, strB, indexB, length)](http://msdn.microsoft.com/en-us/library/es986b3k%28v=vs.110%29.aspx) instead to avoid allocating & extracting all those substrings. – dbc Jul 24 '14 at 07:35

3 Answers3

1

In some cases, an equivalent switch statement is slower than an if-statement or chain of if-statements. Using frequency heuristics, you can optimize a fast path with an if-statement in many programs.

See this Link you will find two different comparisons

http://www.dotnetperls.com/if-switch-performance

Shekhar Pankaj
  • 9,065
  • 3
  • 28
  • 46
0

My philosophy when such a question pops up : the less you write, the better.

Why such a philosophy? One word : testability.
Everytime you add a line, you have to be sure its behaviour is tested. I do not particulary like the switch synthax but when it divides the line number by two, I take it.

artragis
  • 3,677
  • 1
  • 18
  • 30
0

I would go for:

 dayStr = item.Datum.Substring(5, 5))
 day = 'split '-', ',' and convert to int'
 switch day:
 {
    case 20: // Fall through
    case 21:
       // Do something

    case 22:
    ...
}
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119