note 40608 added to function.str-pad
| From: | giorgio dot balestrieri at mail dot wind dot it | Date: | Tue, 09 Mar 2004 13:49:47 +0000 |
| Subject: | note 40608 added to function.str-pad | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-66489@lists.php.net to get a copy of this message | ||
For number formatting (like writing numbers with leading zeroes etc.) , sprintf is much faster than
str_pad.
Consider the following snippet (it take some minutes to run):
<?
echo "str_pad test started, please wait...\n";
$intStart = time();
for ($idx = 1; $idx <= 10000000; $idx++)
$strFoo = str_pad($idx, 10, "0", STR_PAD_LEFT);
$intEnd = time();
echo "str_pad cycle completed in " . ($intEnd - $intStart) . " seconds\n";
echo "sprintf test started, please wait...\n";
$intStart = time();
for ($idx = 1; $idx <= 10000000; $idx++)
$strFoo = sprintf("%010d", $idx);
$intEnd = time();
echo "sprintf cycle completed in " . ($intEnd - $intStart) . " seconds\n";
?>
The str_pad cyle runs 80-100% slower on my pc...
----
Manual Page -- http://www.php.net/manual/en/function.str-pad.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40608
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40608&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40608&report=yes
Search -- http://master.php.net/manage/user-notes.php