note 53526 modified in control-structures.switch by jome

From: Date: Tue, 18 Oct 2005 19:34:02 +0000
Subject: note 53526 modified in control-structures.switch by jome
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-97188@lists.php.net to get a copy of this message
[Editor's note: Changed the second switch to make it work as intended.] I haven't seen anything specifically pointing this out, but you can get a small performance increase in your code by re-structuring your complex switch statements. For example, I was using the following switch to convert textual month names into their numerical counterparts from the Date header of email on my pop server: switch ($month_name) { case "Jan": case "January": $month = "1"; break; ... } Even just looping through 15 emails on the server, it would take upwards of around 9-10 seconds! So, I decided to shorten my switch statement to something like this: switch (TRUE) { case ($month_name == "Jan" || $month_name == "January"): $month = "1"; break; ... } Doing this I actually shaved 3 seconds from my script's execution time!!! I thuoght this was well worth noting for other coders out there who are looking to optimize their PHP code. --was-- I haven't seen anything specifically pointing this out, but you can get a small performance increase in your code by re-structuring your complex switch statements. For example, I was using the following switch to convert textual month names into their numerical counterparts from the Date header of email on my pop server: switch ($month_name) { case "Jan": case "January": $month = "1"; break; ... } Even just looping through 15 emails on the server, it would take upwards of around 9-10 seconds! So, I decided to shorten my switch statement to something like this: switch ($month_name) { case ("Jan" || "January"): $month = "1"; break; ... } Doing this I actually shaved 3 seconds from my script's execution time!!! I thuoght this was well worth noting for other coders out there who are looking to optimize their PHP code. http://php.net/manual/en/control-structures.switch.php

« previous php.notes (#97188) next »