Improve performance of loops. Precalculate limit.

This commit is contained in:
Leonel Quinteros 2013-02-26 12:18:20 -03:00
parent a56504f310
commit 66b7720028
2 changed files with 8 additions and 6 deletions

View File

@ -141,7 +141,8 @@ class Toml
$openBrackets = 0;
$openString = false;
for($i = 0; $i < strlen($toml); $i++)
$strLen = strlen($toml);
for($i = 0; $i < $strLen; $i++)
{
$keep = true;
@ -287,7 +288,8 @@ class Toml
$openString = false;
$buffer = '';
for($i = 0; $i < strlen($val); $i++)
$strLen = strlen($val);
for($i = 0; $i < $strLen; $i++)
{
if($val[$i] == '[' && !$openString)
{

View File

@ -9,7 +9,7 @@ $result = Toml::parseFile('extended.toml');
$one = microtime(true);
echo "\nTooks " . (($one - $start) * 1000) . "s to parse 1 file";
echo "\nTooks " . ($one - $start) . "s to parse 1 file";
echo "\nMemory usage at this point: " . memory_get_usage();
@ -22,19 +22,19 @@ for($i = 0; $i < 1000; $i++)
$thousand = microtime(true);
echo "\nTooks " . (($thousand - $start) * 1000) . "s to parse 1000 times the same file";
echo "\nTooks " . ($thousand - $start) . "s to parse 1000 times the same file";
echo "\nMemory usage at this point: " . memory_get_usage();
$start = microtime(true);
for($i = 0; $i < 100000; $i++)
for($i = 0; $i < 10000; $i++)
{
$result = Toml::parseFile('extended.toml');
}
$thousand = microtime(true);
echo "\nTooks " . (($thousand - $start) * 1000) . "s to parse 100.000 times the same file";
echo "\nTooks " . ($thousand - $start) . "s to parse 10.000 times the same file";
echo "\nMemory usage at this point: " . memory_get_usage();
echo "\n";