note 91622 added to function.intval
| From: | alessandro at live dot it | Date: | Fri, 19 Jun 2009 21:09:23 +0000 |
| Subject: | note 91622 added to function.intval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157142@lists.php.net to get a copy of this message | ||
intval() is a fundamental function, also for security purposes (it is perfect to prevent
SQL-injections etc)...
But...
This is astonishing!
Try this benchmark:
<?php
$result = array(1 => array(), 2 => array());
for($j = 0; $j < 5; $j++)
{
$id = 0;
$start = microtime(true);
for($i = 0; $i < 1000000; $i++)
{
$id = intval('222');
}
$end = microtime(true);
echo $end-$start;
echo "\n";
var_dump($id);
$result[1][] = $end-$start;
$id = 0;
$start = microtime(true);
for($i = 0; $i < 1000000; $i++)
{
$id = (int) '222';
}
$end = microtime(true);
echo $end-$start;
echo "\n";
var_dump($id);
$result[2][] = $end-$start;
}
echo "\n";
$avg[1] = array_sum($result[1]) / count($result[1]);
$avg[2] = array_sum($result[2]) / count($result[2]);
$min = min($avg);
echo "Test 1 avg: ".$avg[1];
echo "\n";
echo "Relative increment: ".((($avg[1] - $min) / $min) * 100)."%";
echo "\n\n";
echo "Test 2 avg: ".$avg[2];
echo "\n";
echo "Relative increment: ".((($avg[2] - $min) / $min) * 100)."%";
echo "\n\n";
?>
Result:
Test 1 avg: 0.51030764579773
Relative increment: 117.22726243067%
Test 2 avg: 0.23491878509521
Relative increment: 0%
That is! intval() is 117% SLOWER than using (int)!
They produce the same result, but the function takes at least twice the time...
Remember this when you write PHP code!
----
Server IP: 213.92.11.19
Probable Submitter: 79.25.175.158
----
Manual Page -- http://www.php.net/manual/en/function.intval.php
Edit -- https://master.php.net/note/edit/91622
Del: integrated -- https://master.php.net/note/delete/91622/integrated
Del: useless -- https://master.php.net/note/delete/91622/useless
Del: bad code -- https://master.php.net/note/delete/91622/bad+code
Del: spam -- https://master.php.net/note/delete/91622/spam
Del: non-english -- https://master.php.net/note/delete/91622/non-english
Del: in docs -- https://master.php.net/note/delete/91622/in+docs
Del: other reasons-- https://master.php.net/note/delete/91622
Reject -- https://master.php.net/note/reject/91622
Search -- https://master.php.net/manage/user-notes.php