Re: [SMARTY] Bugfixes and some other stuff...

From: Date: Sun, 14 Jul 2002 16:50:07 +0000
Subject: Re: [SMARTY] Bugfixes and some other stuff...
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5297@lists.php.net to get a copy of this message
.SMK. wrote:
Instead of writing: switch ($tag_command) { case 'include':
       return $this->_compile_include_tag($tag_args);
case 'include_php':
       return $this->_compile_include_php_tag($tag_args);
... why not using PHP's fast hashing algorithm using a lookup table: $_TAGS = array( 'include' => 1, 'include_php' => 2, 'if' => 3, ... ); switch ($_TAGS[$tag_command]) { case 1:
       return $this->_compile_include_tag($tag_args);
case 2:
       return $this->_compile_include_php_tag($tag_args);
case 3:
       return $this->_compile_if_tag($tag_args);
... With only two test CASEs, the code is 5% faster (almost nothing), but with 4 CASEs, the code executes about 30% faster.
My test case shows the opposite:
      $var = "foo";
       for($x=0; $x<100000; $x++) {
       switch($foo) {
               case "foo1":
               case "foo2":
               case "foo3":
               case "foo4":
               case "foo5":
       }
       }
result: 2.410 seconds
       $var = "foo";
       $_TAGS = array('foo1'=>1,'foo2'=>2,'foo3'=>3,'foo4'=>4);
       for($x=0; $x<100000; $x++) {
       switch($_TAGS[$var]) {
               case 1:
               case 2:
               case 3:
               case 4:
               case 5:
       }
       }
result: 3.003 seconds. So using the strings directly seems faster in this case, which makes sense because the array element doesn't need to be looked up. Monte

« previous php.smarty.general (#5297) next »