note 40608 modified in function.str-pad by danbrown

From: Date: Sat, 02 Jul 2011 18:57:03 +0000
Subject: note 40608 modified in function.str-pad by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181200@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): <?php 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... --was-- 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... http://php.net/manual/en/function.str-pad.php

« previous php.notes (#181200) next »