Bench PHP en dessert
mercredi 8 février 2006 | Webdesign
Puisque cela faisait longtemps...

Il est plus performant d'écrire :
echo '<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="description" content="';
echo $meta_description;
echo '" />
<meta name="keywords" content="';
echo $meta_keywords;
echo '" />
<link rel="alternate" type="application/rss+xml" title="';
echo $rss_title;
echo ' RSS" href="rss.php" />
<link rel="alternate" type="application/rss+xml" title="';
echo $rss_title;
echo ' RSS 2.0" href="rss2.php" />
<link rel="shortcut icon" type="images/x-icon" href="favicon.ico" />
<meta name="DC.title" content="';
echo $title;
echo '" /></head>';
... que d'écrire :
echo '<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="description" content="'.$meta_description.'" />
<meta name="keywords" content="'.$meta_keywords.'" />
<link rel="alternate" type="application/rss+xml" title="'.$title.' RSS" href="rss.php" />
<link rel="alternate" type="application/rss+xml" title="'.$title.' RSS 2.0" href="rss2.php" />
<link rel="shortcut icon" type="images/x-icon" href="favicon.ico" />
<meta name="DC.title" content="'.$title.'" /></head>';
Ceci provient de la concaténation préalable de la chaîne de caractères à afficher. On pourrait préjuger qu'une somme d'instructions multiples (echo
ici même) nuirait aux performances. Or la première solution s'en sort un peu mieux. Moralité, si vous avez de longues chaînes aux pieds, allez-y petit à petit, au lieu de tout concaténer. Bien sûr il s'agit là encore d'une optimisation au niveau atomique, il y a bien d'autres façons de miner un script au C4.