View Single Post
  #15  
Old 08-21-2012, 10:18 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by xXziroXx View Post
Using a switch for params is always a great idea. The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases. Has very little to do with personal preference, and more to do with optimization.
Personal preference and code readability (clarity) are the only things that matter here. The difference in performance between a switch statement and an if-statement for a couple of conditions is going to be ridiculously trivial and totally negligible.

With that said, I have nothing against switch statements, I just didn't want OP to feel that they were needed every time you need to check a condition.

edit: in fact, at 100,000 iterations, an if loop just barely wins (this is repeatable):

Quote:
if: 0.023976087
switch: 0.026674032
PHP Code:
this.maxlooplimit 100000;
temp.foo "foo";

// test if
temp.start timevar2;

for (
temp.0temp.this.maxlooplimittemp.++) {
  if (
temp.foo == "bar") {
    
// bar
  
} else if (temp.foo == "foo") {
    
// foo
  
} else {
    
// neither
  
}
}

echo(
"if: " @ (timevar2 temp.start));

// test switch
temp.start timevar2;

for (
temp.0temp.this.maxlooplimittemp.++) {
  switch (
temp.foo) {
    case 
"bar":
      
// bar
    
break;
    
    case 
"foo":
      
// foo
    
break;
    
    default:
      
// neither
    
break;
  }
}

echo(
"switch: " @ (timevar2 temp.start)); 
...but again, these differences are completely negligable.
__________________
Reply With Quote