note 36875 added to language.constants
| From: | kumar at rn2 dot php dot net | Date: | Sat, 25 Oct 2003 21:59:40 +0000 |
| Subject: | note 36875 added to language.constants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-59289@lists.php.net to get a copy of this message | ||
before embarking on creating a language system I wanted to see if there was any speed advantage to
defining language strings as constants vs. variables or array items. It is more logical to define
language strings as constants but you have more flexibility using variables or arrays in your code
(i.e. they can be accessed directly, concatenated, used in quotes, used in heredocs whereas
constants can only be accessed directly or concatenated).
Results of the test:
declaring as $Variable is fastest
declaring with define() is second fastest
declaring as $Array['Item'] is slowest
=======================================
the test was done using PHP 4.3.2, Apache 1.3.27, and the ab (apache bench) tool.
100 requests (1 concurrent) were sent to one php file that includes 15 php files each containing 100
unique declarations of a language string.
Example of each declaration ("Variable" numbered 1 - 1500):
<?php
$GLOBALS['Variable1'] = "A whole lot of text for this variable as if it were a
language string containing a whole lot of text";
?>
<?php
define('Variable1' , "A whole lot of text for this variable as if it were a language
string containing a whole lot of text");
?>
<?php
$GLOBALS['CP_Lang']['Variable1'] = "A whole lot of text for this variable
as if it were a language string containing a whole lot of text";
?>
Here are the exact averages of each ab run of 100 requests (averages based on 6 runs):
variable (24.956 secs)
constant (25.426 secs)
array (28.141)
(not huge differences but good to know that using variables won't take a huge performance hit)
----
Manual Page -- http://www.php.net/manual/en/language.constants.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+36875
Delete -- http://master.php.net/manage/user-notes.php?action=delete+36875&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+36875&report=yes
Search -- http://master.php.net/manage/user-notes.php