note 29116 added to control-structures.switch
| From: | bart at NOSPAM dot mediawave dot nl | Date: | Mon, 03 Feb 2003 11:25:56 +0000 |
| Subject: | note 29116 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-43320@lists.php.net to get a copy of this message | ||
I did some performance testing comparing a series of if/elseif statements against the switch
statement. The system was a Pentium II 266 running the ISAPI version of PHP 4.3 on IIS. This was the
code:
for ($i = 0; $i < 10000; $i++) {
if ($bla == 1) { $bla = 2;
} elseif ($bla == 2) { $bla = 3;
} elseif ($bla == 3) { $bla = 4;
} elseif ($bla == 4) { $bla = 1;
} else { $bla = 1;
}
}
for ($i = 0; $i < 10000; $i++) {
switch ($bla) {
case 1: $bla = 2; break;
case 2: $bla = 3; break;
case 3: $bla = 4; break;
case 4: $bla = 1; break;
default: $bla = 1;
}
}
if/elseif took an average of 0.085 seconds.
switch took an average of 0.097 seconds.
--
http://www.php.net/manual/en/control-structures.switch.php
http://master.php.net/manage/user-notes.php?action=edit+29116
http://master.php.net/manage/user-notes.php?action=delete+29116
http://master.php.net/manage/user-notes.php?action=reject+29116