Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Author Topic: Using php to render plain text or (x)html - About efficiency  (Read 6348 times)

Joost

  • Guest

The main task of a content management system like sNews, is generating text.
The topic php coding - why the *really* wide lines? got me (and others as well) thinking. What is the most efficient way to render text?

Since I am in the optimizing mood/mode, so I ran some tests. I compared 3 versions of function settings . The original version (6 kb) executes function html_input (1.5 kb) 33 times. Knowing that it will only generate 6kb of text/html and knowing that calling functions creates a lot of overhead, this is, of course, the big looser.

I wrote two replacements, on using echo.
The other using <?php and ?>, like it is done in index.php. These tokens are used to let php know when to parse the characters and when to treat it like plain text. This method, is the absolute winner. All setings versions are tested in a sNews install Here are the results:

Using php/text mode ( <?php)

memory_get_peak_usage(true) 1835008 bytes
memory_get_peak_usage(false) 1739616 bytes
memory_get_usage(false) 1564336 bytes

Using ECHO

memory_get_peak_usage(true) 2097152 bytes
memory_get_peak_usage(false) 1723408 bytes
memory_get_usage(false) 1548152 bytes

Using function html_input

memory_get_peak_usage(true) 2097152 bytes
memory_get_peak_usage(false) 1790648 bytes
memory_get_usage(false) 1615368 bytes

Using php/text mode ( <?php)

Code: [Select]
<?php
// SETTINGS FORM
function settings() {
$select ' selected="selected"';
$check 'checked = "checked"';
$lang s('language') == '' 'EN' s('language');
?>

<form method="post" action="index.php?action=process&amp;task=save_settings" accept-charset="UTF-8">
<fieldset>
<legend><a onclick="toggle('sub1')" style="cursor: pointer;"><?php echo l('settings');?></a></legend>
<div id="sub1" style="display: none;">
<p><label for="webtitle"><?php echo l('a_website_title');?></label>:<br /><input type="text" name="website_title" id="webtitle" value="<?php echo s('website_title');?>" /></p>
<p><label for="wdesc"><?php echo l('a_description');?></label>:<br /><input type="text" name="website_description" id="wdesc" value="<?php echo s('website_description');?>" /></p>
<p><label for="wkey"><?php echo l('a_keywords');?></label>:<br /><input type="text" name="website_keywords" id="wkey" value="<?php echo s('website_keywords');?>" /></p></div></fieldset>

<fieldset>
<legend><a onclick="toggle('sub2')" style="cursor: pointer;"><?php echo l('a_contact_info');?></a></legend>
<div id="sub2" style="display: none;"><p><label for="we"><?php echo l('a_website_email');?></label>:<br /><input type="text" name="website_email" id="we" value="<?php echo s('website_email');?>" /></p>
<p><label for="cs"><?php echo l('a_contact_subject');?></label>:<br /><input type="text" name="contact_subject" id="cs" value="<?php echo s('contact_subject');?>" /></p></div></fieldset>
<fieldset><legend><a onclick="toggle('sub3')" style="cursor: pointer;"><?php echo l('a_time_settings');?></a></legend>
<div id="sub3" style="display: none;">
<p><label for="lang"><?php echo l('a_language');?></label>:<br /><input type="text" name="language" id="lang" value="<?php echo $lang;?>" /></p>
<p><label for="char"><?php echo l('charset');?></label>:<br /><input type="text" name="charset" id="char" value="<?php echo s('charset');?>" /></p>
<p><label for="dt"><?php echo l('a_date_format');?></label>:<br /><input type="text" name="date_format" id="dt" value="<?php echo s('date_format');?>" /></p></div></fieldset>

<fieldset>
<legend><a onclick="toggle('sub4')" style="cursor: pointer;"><?php echo l('contents');?></a></legend>
<div id="sub4" style="display: none;"><p><label for="artl"><?php echo l('a_article_limit');?></label>:<br /><input type="text" name="article_limit" id="artl" value="<?php echo s('article_limit');?>" /></p>
<p><label for="rssl"><?php echo l('a_rss_limit');?></label>:<br /><input type="text" name="rss_limit" id="rssl" value="<?php echo s('rss_limit');?>" /></p>
<p><label for="dp"><?php echo l('a_display_page');?>:</label> <select name="display_page" id="dp">
<option value="0"<?php echo (s('display_page') == $select '');?>><?php echo l('none');?></option>
<?php
$result 
mysql_query('SELECT id,title FROM '._PRE.'articles WHERE position = 3 ORDER BY id ASC');
while (
$r mysql_fetch_array($result)) {
echo 
'<option value="',$r['id'],'"';
if (
s('display_page') == $r['id']) {echo $select;}
echo 
'>',$r['title'],'</option>';
}
?>

</select></p>
<p><input type="checkbox" name="display_new_on_home" id="dnoh"<?php echo (s('display_new_on_home') == 'on' $check '');?> /> <label for="dnoh"><?php echo l('a_display_new_on_home');?></label></p>
<p><input type="checkbox" name="display_pagination" id="dpag" <?php echo (s('display_pagination') == 'on' $check '');?>/> <label for="dpag"><?php echo l('a_display_pagination');?></label></p>
<p><input type="checkbox" name="num_categories" id="nc"<?php echo (s('num_categories') == 'on' $check '');?> /> <label for="nc"><?php echo l('a_num_categories');?></label></p>
<p><input type="checkbox" name="enable_extras" id="ee" <?php echo (s('enable_extras') == 'YES' $check '');?> /> <label for="ee"><?php echo l('enable_extras');?></label></p></div></fieldset>
<fieldset>
<legend><a title="<?php echo l('comments');?>" onclick="toggle('sub5')" style="cursor: pointer;"><?php echo l('comments');?></a></legend>
<div id="sub5" style="display: none;">
<p><label for="co"><?php echo l('a_comments_order');?>:</label><br /><select id="co" name="comments_order">
<option value="DESC"<?php echo (s('comments_order') == 'DESC' $select '');?>><?php echo l('newer_top');?></option>
<option value="ASC"<?php echo (s('comments_order') == 'ASC' $select '');?>><?php echo l('newer_bottom');?></option></select></p>
<p><label for="cl"><?php echo l('a_comment_limit');?></label>:<br /><input type="text" name="comment_limit" id="cl" value="<?php echo s('comment_limit');?>" /></p>
<p><input type="checkbox" name="word_filter_enable" id="wfe" /> <label for="wfe">Enable Badwords filter</label></p>
<p><label for="wff"><?php echo l('a_word_filter_enable');?></label>:<br /><input type="text" name="word_filter_file" id="wff" value="<?php echo s('word_filter_file');?>" /></p>
<p><label for="wfc"><?php echo l('a_word_filter_change');?></label>:<br /><input type="text" name="word_filter_change" id="wfc" <?php echo (s('word_filter_change') == 'on' $check '');?> /></p></div></fieldset>

<p><input type="submit" name="save" id="save" class="button" value="<?php echo l('save');?>" /></p></form>
<form method="post" action="index.php?action=process&amp;task=changeup" accept-charset="UTF-8">
<fieldset><legend><a onclick="toggle('sub6')" style="cursor: pointer;"><?php echo l('change_up');?></a></legend>
<div id="sub6" style="display: none;"><p><?php echo l('login_limit');?></p>
<p><label for="uname"><?php echo  l('a_username');?></label>:<br /><input type="text" name="uname" id="uname" value="" /></p>
<p><label for="pass1"><?php echo l('a_password');?></label>:<br /><input type="password" name="pass1" id="pass1" value="" /></p>
<p><label for="pass2"><?php echo l('a_password2');?></label>:<br /><input type="password" name="pass2" id="pass2" value="" /></p>
<p><input type="hidden" name="task" id="task" value="changeup" />
<input type="submit" name="submit_pass" id="submit_pass" class="button" value="<?php echo l('save');?>" /></p></div></fieldset></form>
<?php
}
?>

Using ECHO

Code: [Select]
<?php
function settings() {
$select ' selected="selected"';
$check 'checked = "checked"';
echo
'<form method="post" action="index.php?action=process&amp;task=save_settings" accept-charset="UTF-8">
<fieldset>
<legend><a onclick="toggle(\'sub1\')" style="cursor: pointer;">'
.l('settings').'</a></legend>
<div id="sub1" style="display: none;">
<p><label for="webtitle">'
.l('a_website_title').'</label>:<br /><input type="text" name="website_title" id="webtitle" value="'.s('website_title').'" /></p>
<p><label for="wdesc">'
.l('a_description').'</label>:<br /><input type="text" name="website_description" id="wdesc" value="'.s('website_description').'" /></p>
<p><label for="wkey">'
.l('a_keywords').'</label>:<br /><input type="text" name="website_keywords" id="wkey" value="'.s('website_keywords').'" /></p></div></fieldset>';

$lang s('language') == '' 'EN' s('language');
echo
'<fieldset>
<legend><a onclick="toggle(\'sub2\')" style="cursor: pointer;">'
.l('a_contact_info').'</a></legend>
<div id="sub2" style="display: none;"><p><label for="we">'
.l('a_website_email').'</label>:<br /><input type="text" name="website_email" id="we" value="'.s('website_email').'" /></p>
<p><label for="cs">'
.l('a_contact_subject').'</label>:<br /><input type="text" name="contact_subject" id="cs" value="'.s('contact_subject').'" /></p></div></fieldset>
<fieldset><legend><a onclick="toggle(\'sub3\')" style="cursor: pointer;">'
.l('a_time_settings').'</a></legend>
<div id="sub3" style="display: none;">
<p><label for="lang">'
.l('a_language').'</label>:<br /><input type="text" name="language" id="lang" value="'.$lang.'" /></p>
<p><label for="char">'
.l('charset').'</label>:<br /><input type="text" name="charset" id="char" value="'.s('charset').'" /></p>
<p><label for="dt">'
.l('a_date_format').'</label>:<br /><input type="text" name="date_format" id="dt" value="'.s('date_format').'" /></p></div></fieldset>';

echo
'<fieldset>
<legend><a onclick="toggle(\'sub4\')" style="cursor: pointer;">'
.l('contents').'</a></legend>
<div id="sub4" style="display: none;"><p><label for="artl">'
.l('a_article_limit').'</label>:<br /><input type="text" name="article_limit" id="artl" value="'.s('article_limit').'" /></p>
<p><label for="rssl">'
.l('a_rss_limit').'</label>:<br /><input type="text" name="rss_limit" id="rssl" value="'.s('rss_limit').'" /></p>
<p><label for="dp">'
.l('a_display_page').':</label> <select name="display_page" id="dp">
<option value="0"'
.(s('display_page') == $select '').'>'.l('none').'</option>';
$result mysql_query('SELECT id,title FROM '._PRE.'articles WHERE position = 3 ORDER BY id ASC');
while (
$r mysql_fetch_array($result)) {
echo 
'<option value="',$r['id'],'"';
if (
s('display_page') == $r['id']) {echo $select;}
echo 
'>',$r['title'],'</option>';
}
echo
'</select></p>
<p><input type="checkbox" name="display_new_on_home" id="dnoh"'
.(s('display_new_on_home') == 'on' $check '').' /> <label for="dnoh">'.l('a_display_new_on_home').'</label></p>
<p><input type="checkbox" name="display_pagination" id="dpag" '
.(s('display_pagination') == 'on' $check '').'/> <label for="dpag">'.l('a_display_pagination').'</label></p>
<p><input type="checkbox" name="num_categories" id="nc"'
.(s('num_categories') == 'on' $check '').' /> <label for="nc">'.l('a_num_categories').'</label></p>
<p><input type="checkbox" name="enable_extras" id="ee" '
.(s('enable_extras') == 'YES' $check '').' /> <label for="ee">'.l('enable_extras').'</label></p></div></fieldset>
<fieldset>
<legend><a title="'
.l('comments').'" onclick="toggle(\'sub5\')" style="cursor: pointer;">'.l('comments').'</a></legend>
<div id="sub5" style="display: none;">
<p><label for="co">'
.l('a_comments_order').':</label><br /><select id="co" name="comments_order">
<option value="DESC"'
.(s('comments_order') == 'DESC' $select '').'>'.l('newer_top').'</option>
<option value="ASC"'
.(s('comments_order') == 'ASC' $select '').'>'.l('newer_bottom').'</option></select></p>
<p><label for="cl">'
.l('a_comment_limit').'</label>:<br /><input type="text" name="comment_limit" id="cl" value="'.s('comment_limit').'" /></p>
<p><input type="checkbox" name="word_filter_enable" id="wfe" /> <label for="wfe">Enable Badwords filter</label></p>
<p><label for="wff">'
.l('a_word_filter_enable').'</label>:<br /><input type="text" name="word_filter_file" id="wff" value="'.s('word_filter_file').'" /></p>
<p><label for="wfc">'
.l('a_word_filter_change').'</label>:<br /><input type="text" name="word_filter_change" id="wfc" '.(s('word_filter_change') == 'on' $check '').' /></p></div></fieldset>';

echo
'<p><input type="submit" name="save" id="save" class="button" value="'.l('save').'" /></p></form>
<form method="post" action="index.php?action=process&amp;task=changeup" accept-charset="UTF-8">
<fieldset><legend><a onclick="toggle(\'sub6\')" style="cursor: pointer;">'
.l('change_up').'</a></legend>
<div id="sub6" style="display: none;"><p>'
.l('login_limit').'</p>
<p><label for="uname">'
l('a_username').'</label>:<br /><input type="text" name="uname" id="uname" value="" /></p>
<p><label for="pass1">'
.l('a_password').'</label>:<br /><input type="password" name="pass1" id="pass1" value="" /></p>
<p><label for="pass2">'
.l('a_password2').'</label>:<br /><input type="password" name="pass2" id="pass2" value="" /></p>
<p><input type="hidden" name="task" id="task" value="changeup" />
<input type="submit" name="submit_pass" id="submit_pass" class="button" value="'
.l('save').'" /></p></div></fieldset></form>';
}
?>

Using function html_input

To be found in snews.php
« Last Edit: April 20, 2008, 04:51:35 pm by Joost »
Logged