Quote:
Originally Posted by xXziroXx
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.i = 0; temp.i < this.maxlooplimit; temp.i ++) {
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.i = 0; temp.i < this.maxlooplimit; temp.i ++) {
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.