optimized for loops with count() function calls

This commit is contained in:
mohrt
2002-06-03 16:05:33 +00:00
parent f1d75e32c8
commit 51a14712f8
11 changed files with 40 additions and 29 deletions

1
NEWS
View File

@@ -1,3 +1,4 @@
- optimized for loops with count() function calls (monte)
- add month_value_format attribute to html_select_date - add month_value_format attribute to html_select_date
plugin (Gary Loescher, Monte) plugin (Gary Loescher, Monte)
- made it possible to use simple variables inside [] for - made it possible to use simple variables inside [] for

View File

@@ -1157,7 +1157,7 @@ function _generate_debug_output() {
$this->_config[1]['files'][$file] = true; $this->_config[1]['files'][$file] = true;
} }
} else if ($scope == 'global') } else if ($scope == 'global')
for ($i = 1; $i < count($this->_config); $i++) { for ($i = 1, $for_max = count($this->_config); $i < $for_max; $i++) {
if (!isset($this->_config[$i]['files'][$file])) { if (!isset($this->_config[$i]['files'][$file])) {
$this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file)); $this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file));
$this->_config[$i]['files'][$file] = true; $this->_config[$i]['files'][$file] = true;
@@ -1170,7 +1170,7 @@ function _generate_debug_output() {
if (count($this->_config) > 0) if (count($this->_config) > 0)
$this->_config[1]['vars'] = array_merge($this->_config[1]['vars'], $this->_conf_obj->get($file, $section)); $this->_config[1]['vars'] = array_merge($this->_config[1]['vars'], $this->_conf_obj->get($file, $section));
} else if ($scope == 'global') } else if ($scope == 'global')
for ($i = 1; $i < count($this->_config); $i++) for ($i = 1, $for_max = count($this->_config); $i < $for_max; $i++)
$this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file, $section)); $this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file, $section));
} }
@@ -1194,7 +1194,7 @@ function _generate_debug_output() {
$results, $match); $results, $match);
list($cached_inserts, $insert_args) = $match; list($cached_inserts, $insert_args) = $match;
for ($i = 0; $i < count($cached_inserts); $i++) { for ($i = 0, $for_max = count($cached_inserts); $i < $for_max; $i++) {
if ($this->debugging) { if ($this->debugging) {
$debug_start_time = $this->_get_microtime(); $debug_start_time = $this->_get_microtime();
} }

View File

@@ -110,11 +110,11 @@ class Smarty_Compiler extends Smarty {
$text_blocks = preg_split("!{$ldq}.*?{$rdq}!s", $template_source); $text_blocks = preg_split("!{$ldq}.*?{$rdq}!s", $template_source);
/* loop through text blocks */ /* loop through text blocks */
for ($curr_tb = 0; $curr_tb < count($text_blocks); $curr_tb++) { for ($curr_tb = 0, $for_max = count($text_blocks); $curr_tb < $for_max; $curr_tb++) {
/* match anything within <? ?> */ /* match anything within <? ?> */
if (preg_match_all('!(<\?[^?]*?\?>|<script\s+language\s*=\s*[\"\']?php[\"\']?\s*>)!is', $text_blocks[$curr_tb], $sp_match)) { if (preg_match_all('!(<\?[^?]*?\?>|<script\s+language\s*=\s*[\"\']?php[\"\']?\s*>)!is', $text_blocks[$curr_tb], $sp_match)) {
/* found at least one match, loop through each one */ /* found at least one match, loop through each one */
for ($curr_sp = 0; $curr_sp < count($sp_match[0]); $curr_sp++) { for ($curr_sp = 0, $for_max2 = count($sp_match[0]); $curr_sp < $for_max2; $curr_sp++) {
if (preg_match('!^(<\?(php\s|\s|=\s)|<script\s*language\s*=\s*[\"\']?php[\"\']?\s*>)!is', $sp_match[0][$curr_sp])) { if (preg_match('!^(<\?(php\s|\s|=\s)|<script\s*language\s*=\s*[\"\']?php[\"\']?\s*>)!is', $sp_match[0][$curr_sp])) {
/* php tag */ /* php tag */
if ($this->php_handling == SMARTY_PHP_PASSTHRU) { if ($this->php_handling == SMARTY_PHP_PASSTHRU) {
@@ -140,7 +140,7 @@ class Smarty_Compiler extends Smarty {
/* Compile the template tags into PHP code. */ /* Compile the template tags into PHP code. */
$compiled_tags = array(); $compiled_tags = array();
for ($i = 0; $i < count($template_tags); $i++) { for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++) {
$this->_current_line_no += substr_count($text_blocks[$i], "\n"); $this->_current_line_no += substr_count($text_blocks[$i], "\n");
$compiled_tags[] = $this->_compile_tag($template_tags[$i]); $compiled_tags[] = $this->_compile_tag($template_tags[$i]);
$this->_current_line_no += substr_count($template_tags[$i], "\n"); $this->_current_line_no += substr_count($template_tags[$i], "\n");
@@ -149,7 +149,7 @@ class Smarty_Compiler extends Smarty {
$template_compiled = ''; $template_compiled = '';
/* Interleave the compiled contents and text blocks to get the final result. */ /* Interleave the compiled contents and text blocks to get the final result. */
for ($i = 0; $i < count($compiled_tags); $i++) { for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) {
$template_compiled .= $text_blocks[$i].$compiled_tags[$i]; $template_compiled .= $text_blocks[$i].$compiled_tags[$i];
} }
$template_compiled .= $text_blocks[$i]; $template_compiled .= $text_blocks[$i];
@@ -159,7 +159,7 @@ class Smarty_Compiler extends Smarty {
$strip_tags = $match[0]; $strip_tags = $match[0];
$strip_tags_modified = preg_replace("!{$ldq}/?strip{$rdq}|[\t ]+$|^[\t ]+!m", '', $strip_tags); $strip_tags_modified = preg_replace("!{$ldq}/?strip{$rdq}|[\t ]+$|^[\t ]+!m", '', $strip_tags);
$strip_tags_modified = preg_replace('![\r\n]+!m', '', $strip_tags_modified); $strip_tags_modified = preg_replace('![\r\n]+!m', '', $strip_tags_modified);
for ($i = 0; $i < count($strip_tags); $i++) for ($i = 0, $for_max = count($strip_tags); $i < $for_max; $i++)
$template_compiled = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $template_compiled = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s",
$this->quote_replace($strip_tags_modified[$i]), $this->quote_replace($strip_tags_modified[$i]),
$template_compiled, 1); $template_compiled, 1);
@@ -844,7 +844,7 @@ class Smarty_Compiler extends Smarty {
$is_arg_stack = array(); $is_arg_stack = array();
for ($i = 0; $i < count($tokens); $i++) { for ($i = 0, $for_max = count($tokens); $i < $for_max; $i++) {
$token = &$tokens[$i]; $token = &$tokens[$i];
switch ($token) { switch ($token) {
@@ -1219,7 +1219,7 @@ class Smarty_Compiler extends Smarty {
preg_match_all('!\|(@?\w+)((?>:(?:'. $qstr_regexp . '|[^|]+))*)!', '|' . $modifier_string, $match); preg_match_all('!\|(@?\w+)((?>:(?:'. $qstr_regexp . '|[^|]+))*)!', '|' . $modifier_string, $match);
list(, $modifiers, $modifier_arg_strings) = $match; list(, $modifiers, $modifier_arg_strings) = $match;
for ($i = 0; $i < count($modifiers); $i++) { for ($i = 0, $for_max = count($modifiers); $i < $for_max; $i++) {
$modifier_name = $modifiers[$i]; $modifier_name = $modifiers[$i];
preg_match_all('!:(' . $qstr_regexp . '|[^:]+)!', $modifier_arg_strings[$i], $match); preg_match_all('!:(' . $qstr_regexp . '|[^:]+)!', $modifier_arg_strings[$i], $match);
$modifier_args = $match[1]; $modifier_args = $match[1];

View File

@@ -400,7 +400,7 @@ href="mailto:%62%6f%62%40%6d%65%2e%6e%65%74"&gt;&amp;#x62;&amp;#x6f;&amp;#x62;&a
<para> <para>
Smarty's homepage is located at http://www.phpinsider.com/php/code/Smarty/. Smarty's homepage is located at http://www.phpinsider.com/php/code/Smarty/.
You can join the mailing list by sending an e-mail to You can join the mailing list by sending an e-mail to
subscribe-smarty@lists.ispi.net. An archive of the mailing list can be smarty-general-subscribe@lists.php.net. An archive of the mailing list can be
viewed at http://marc.theaimsgroup.com/?l=smarty&amp;r=1&amp;w=2 viewed at http://marc.theaimsgroup.com/?l=smarty&amp;r=1&amp;w=2
</para> </para>
</chapter> </chapter>

View File

@@ -3737,6 +3737,16 @@ OUTPUT:
math function will be assigned to this template variable instead of math function will be assigned to this template variable instead of
being output to the template. being output to the template.
</para> </para>
<note>
<title>Technical Note</title>
<para>
math is an expensive function in performance due to its use of
the php eval() function. Doing the math in PHP is much more
efficient, so whenever possible do the math calculations in PHP
and assign the results to the template. Definately avoid
repetitive math function calls, like within section loops.
</para>
</note>
<example> <example>
<title>math</title> <title>math</title>
<programlisting> <programlisting>

View File

@@ -1157,7 +1157,7 @@ function _generate_debug_output() {
$this->_config[1]['files'][$file] = true; $this->_config[1]['files'][$file] = true;
} }
} else if ($scope == 'global') } else if ($scope == 'global')
for ($i = 1; $i < count($this->_config); $i++) { for ($i = 1, $for_max = count($this->_config); $i < $for_max; $i++) {
if (!isset($this->_config[$i]['files'][$file])) { if (!isset($this->_config[$i]['files'][$file])) {
$this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file)); $this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file));
$this->_config[$i]['files'][$file] = true; $this->_config[$i]['files'][$file] = true;
@@ -1170,7 +1170,7 @@ function _generate_debug_output() {
if (count($this->_config) > 0) if (count($this->_config) > 0)
$this->_config[1]['vars'] = array_merge($this->_config[1]['vars'], $this->_conf_obj->get($file, $section)); $this->_config[1]['vars'] = array_merge($this->_config[1]['vars'], $this->_conf_obj->get($file, $section));
} else if ($scope == 'global') } else if ($scope == 'global')
for ($i = 1; $i < count($this->_config); $i++) for ($i = 1, $for_max = count($this->_config); $i < $for_max; $i++)
$this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file, $section)); $this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file, $section));
} }
@@ -1194,7 +1194,7 @@ function _generate_debug_output() {
$results, $match); $results, $match);
list($cached_inserts, $insert_args) = $match; list($cached_inserts, $insert_args) = $match;
for ($i = 0; $i < count($cached_inserts); $i++) { for ($i = 0, $for_max = count($cached_inserts); $i < $for_max; $i++) {
if ($this->debugging) { if ($this->debugging) {
$debug_start_time = $this->_get_microtime(); $debug_start_time = $this->_get_microtime();
} }

View File

@@ -110,11 +110,11 @@ class Smarty_Compiler extends Smarty {
$text_blocks = preg_split("!{$ldq}.*?{$rdq}!s", $template_source); $text_blocks = preg_split("!{$ldq}.*?{$rdq}!s", $template_source);
/* loop through text blocks */ /* loop through text blocks */
for ($curr_tb = 0; $curr_tb < count($text_blocks); $curr_tb++) { for ($curr_tb = 0, $for_max = count($text_blocks); $curr_tb < $for_max; $curr_tb++) {
/* match anything within <? ?> */ /* match anything within <? ?> */
if (preg_match_all('!(<\?[^?]*?\?>|<script\s+language\s*=\s*[\"\']?php[\"\']?\s*>)!is', $text_blocks[$curr_tb], $sp_match)) { if (preg_match_all('!(<\?[^?]*?\?>|<script\s+language\s*=\s*[\"\']?php[\"\']?\s*>)!is', $text_blocks[$curr_tb], $sp_match)) {
/* found at least one match, loop through each one */ /* found at least one match, loop through each one */
for ($curr_sp = 0; $curr_sp < count($sp_match[0]); $curr_sp++) { for ($curr_sp = 0, $for_max2 = count($sp_match[0]); $curr_sp < $for_max2; $curr_sp++) {
if (preg_match('!^(<\?(php\s|\s|=\s)|<script\s*language\s*=\s*[\"\']?php[\"\']?\s*>)!is', $sp_match[0][$curr_sp])) { if (preg_match('!^(<\?(php\s|\s|=\s)|<script\s*language\s*=\s*[\"\']?php[\"\']?\s*>)!is', $sp_match[0][$curr_sp])) {
/* php tag */ /* php tag */
if ($this->php_handling == SMARTY_PHP_PASSTHRU) { if ($this->php_handling == SMARTY_PHP_PASSTHRU) {
@@ -140,7 +140,7 @@ class Smarty_Compiler extends Smarty {
/* Compile the template tags into PHP code. */ /* Compile the template tags into PHP code. */
$compiled_tags = array(); $compiled_tags = array();
for ($i = 0; $i < count($template_tags); $i++) { for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++) {
$this->_current_line_no += substr_count($text_blocks[$i], "\n"); $this->_current_line_no += substr_count($text_blocks[$i], "\n");
$compiled_tags[] = $this->_compile_tag($template_tags[$i]); $compiled_tags[] = $this->_compile_tag($template_tags[$i]);
$this->_current_line_no += substr_count($template_tags[$i], "\n"); $this->_current_line_no += substr_count($template_tags[$i], "\n");
@@ -149,7 +149,7 @@ class Smarty_Compiler extends Smarty {
$template_compiled = ''; $template_compiled = '';
/* Interleave the compiled contents and text blocks to get the final result. */ /* Interleave the compiled contents and text blocks to get the final result. */
for ($i = 0; $i < count($compiled_tags); $i++) { for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) {
$template_compiled .= $text_blocks[$i].$compiled_tags[$i]; $template_compiled .= $text_blocks[$i].$compiled_tags[$i];
} }
$template_compiled .= $text_blocks[$i]; $template_compiled .= $text_blocks[$i];
@@ -159,7 +159,7 @@ class Smarty_Compiler extends Smarty {
$strip_tags = $match[0]; $strip_tags = $match[0];
$strip_tags_modified = preg_replace("!{$ldq}/?strip{$rdq}|[\t ]+$|^[\t ]+!m", '', $strip_tags); $strip_tags_modified = preg_replace("!{$ldq}/?strip{$rdq}|[\t ]+$|^[\t ]+!m", '', $strip_tags);
$strip_tags_modified = preg_replace('![\r\n]+!m', '', $strip_tags_modified); $strip_tags_modified = preg_replace('![\r\n]+!m', '', $strip_tags_modified);
for ($i = 0; $i < count($strip_tags); $i++) for ($i = 0, $for_max = count($strip_tags); $i < $for_max; $i++)
$template_compiled = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $template_compiled = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s",
$this->quote_replace($strip_tags_modified[$i]), $this->quote_replace($strip_tags_modified[$i]),
$template_compiled, 1); $template_compiled, 1);
@@ -844,7 +844,7 @@ class Smarty_Compiler extends Smarty {
$is_arg_stack = array(); $is_arg_stack = array();
for ($i = 0; $i < count($tokens); $i++) { for ($i = 0, $for_max = count($tokens); $i < $for_max; $i++) {
$token = &$tokens[$i]; $token = &$tokens[$i];
switch ($token) { switch ($token) {
@@ -1219,7 +1219,7 @@ class Smarty_Compiler extends Smarty {
preg_match_all('!\|(@?\w+)((?>:(?:'. $qstr_regexp . '|[^|]+))*)!', '|' . $modifier_string, $match); preg_match_all('!\|(@?\w+)((?>:(?:'. $qstr_regexp . '|[^|]+))*)!', '|' . $modifier_string, $match);
list(, $modifiers, $modifier_arg_strings) = $match; list(, $modifiers, $modifier_arg_strings) = $match;
for ($i = 0; $i < count($modifiers); $i++) { for ($i = 0, $for_max = count($modifiers); $i < $for_max; $i++) {
$modifier_name = $modifiers[$i]; $modifier_name = $modifiers[$i];
preg_match_all('!:(' . $qstr_regexp . '|[^:]+)!', $modifier_arg_strings[$i], $match); preg_match_all('!:(' . $qstr_regexp . '|[^:]+)!', $modifier_arg_strings[$i], $match);
$modifier_args = $match[1]; $modifier_args = $match[1];

View File

@@ -29,7 +29,7 @@ function smarty_function_html_options($params, &$smarty)
} else { } else {
settype($output, 'array'); settype($output, 'array');
settype($values, 'array'); settype($values, 'array');
for ($i = 0; $i < count($output); $i++) { for ($i = 0, $for_max = count($output); $i < $for_max; $i++) {
/* By default, check value against $selected */ /* By default, check value against $selected */
$sel_check = $values[$i]; $sel_check = $values[$i];
$html_result .= "<option"; $html_result .= "<option";

View File

@@ -37,7 +37,7 @@ function smarty_function_html_select_time($params, &$smarty)
if ($display_hours) { if ($display_hours) {
$hours = $use_24_hours ? range(0, 23) : range(1, 12); $hours = $use_24_hours ? range(0, 23) : range(1, 12);
$hour_fmt = $use_24_hours ? '%H' : '%I'; $hour_fmt = $use_24_hours ? '%H' : '%I';
for ($i = 0; $i < count($hours); $i++) for ($i = 0, $for_max = count($hours); $i < $for_max; $i++)
$hours[$i] = sprintf('%02d', $hours[$i]); $hours[$i] = sprintf('%02d', $hours[$i]);
$html_result .= '<select name='; $html_result .= '<select name=';
if (null !== $field_array) { if (null !== $field_array) {
@@ -55,7 +55,7 @@ function smarty_function_html_select_time($params, &$smarty)
if ($display_minutes) { if ($display_minutes) {
$all_minutes = range(0, 59); $all_minutes = range(0, 59);
for ($i = 0; $i < count($all_minutes); $i+= $minute_interval) for ($i = 0, $for_max = count($all_minutes); $i < $for_max; $i+= $minute_interval)
$minutes[] = sprintf('%02d', $all_minutes[$i]); $minutes[] = sprintf('%02d', $all_minutes[$i]);
$selected = intval(floor(strftime('%M', $time) / $minute_interval) * $minute_interval); $selected = intval(floor(strftime('%M', $time) / $minute_interval) * $minute_interval);
$html_result .= '<select name='; $html_result .= '<select name=';
@@ -74,7 +74,7 @@ function smarty_function_html_select_time($params, &$smarty)
if ($display_seconds) { if ($display_seconds) {
$all_seconds = range(0, 59); $all_seconds = range(0, 59);
for ($i = 0; $i < count($all_seconds); $i+= $second_interval) for ($i = 0, $for_max = count($all_seconds); $i < $for_max; $i+= $second_interval)
$seconds[] = sprintf('%02d', $all_seconds[$i]); $seconds[] = sprintf('%02d', $all_seconds[$i]);
$selected = intval(floor(strftime('%S', $time) / $second_interval) * $second_interval); $selected = intval(floor(strftime('%S', $time) / $second_interval) * $second_interval);
$html_result .= '<select name='; $html_result .= '<select name=';

View File

@@ -29,7 +29,7 @@ function smarty_function_html_options($params, &$smarty)
} else { } else {
settype($output, 'array'); settype($output, 'array');
settype($values, 'array'); settype($values, 'array');
for ($i = 0; $i < count($output); $i++) { for ($i = 0, $for_max = count($output); $i < $for_max; $i++) {
/* By default, check value against $selected */ /* By default, check value against $selected */
$sel_check = $values[$i]; $sel_check = $values[$i];
$html_result .= "<option"; $html_result .= "<option";

View File

@@ -37,7 +37,7 @@ function smarty_function_html_select_time($params, &$smarty)
if ($display_hours) { if ($display_hours) {
$hours = $use_24_hours ? range(0, 23) : range(1, 12); $hours = $use_24_hours ? range(0, 23) : range(1, 12);
$hour_fmt = $use_24_hours ? '%H' : '%I'; $hour_fmt = $use_24_hours ? '%H' : '%I';
for ($i = 0; $i < count($hours); $i++) for ($i = 0, $for_max = count($hours); $i < $for_max; $i++)
$hours[$i] = sprintf('%02d', $hours[$i]); $hours[$i] = sprintf('%02d', $hours[$i]);
$html_result .= '<select name='; $html_result .= '<select name=';
if (null !== $field_array) { if (null !== $field_array) {
@@ -55,7 +55,7 @@ function smarty_function_html_select_time($params, &$smarty)
if ($display_minutes) { if ($display_minutes) {
$all_minutes = range(0, 59); $all_minutes = range(0, 59);
for ($i = 0; $i < count($all_minutes); $i+= $minute_interval) for ($i = 0, $for_max = count($all_minutes); $i < $for_max; $i+= $minute_interval)
$minutes[] = sprintf('%02d', $all_minutes[$i]); $minutes[] = sprintf('%02d', $all_minutes[$i]);
$selected = intval(floor(strftime('%M', $time) / $minute_interval) * $minute_interval); $selected = intval(floor(strftime('%M', $time) / $minute_interval) * $minute_interval);
$html_result .= '<select name='; $html_result .= '<select name=';
@@ -74,7 +74,7 @@ function smarty_function_html_select_time($params, &$smarty)
if ($display_seconds) { if ($display_seconds) {
$all_seconds = range(0, 59); $all_seconds = range(0, 59);
for ($i = 0; $i < count($all_seconds); $i+= $second_interval) for ($i = 0, $for_max = count($all_seconds); $i < $for_max; $i+= $second_interval)
$seconds[] = sprintf('%02d', $all_seconds[$i]); $seconds[] = sprintf('%02d', $all_seconds[$i]);
$selected = intval(floor(strftime('%S', $time) / $second_interval) * $second_interval); $selected = intval(floor(strftime('%S', $time) / $second_interval) * $second_interval);
$html_result .= '<select name='; $html_result .= '<select name=';