我試圖讓我的代碼更快,並且我得到了很多If-else和If-或者它。我知道,如果你有超過5個if/case的話,switch case會更快。那麼if-else
與if-or
有多快,它們是一樣的嗎?C#if else vs if或vs switch case
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
}
還是我剛剛與開關罩去?
switch(item.Datum.Substring(5, 5))
{
case "06-20", "06,21":
Something
break;
case "06-22", "06,23":
Something
break;
}
http://stackoverflow.com/questions/395618/is-there-any-significant-difference-between-using-if-else-and-switch-case -in-c – DarkBee
[哪個更快?](http://ericlippert.com/2012/12/17/performance-rant/) –
除了別的,不要多次調用Substring! –