Project

General

Profile

Bug #23525 » 15685_test.php

Administrator Admin, 2010-09-11 16:08

 
<?php

$times = 300000;


function testInt ($var) {
return !strcmp($var, intval($var));
}
function testIntVariantTrunk ($var) {
return (string)intval($var) == $var;
}
function testIntVariant1 ($var) {
if ($var === '') {
return FALSE;
}
return (string)intval($var) === (string)$var;
}

function testIntVariant2 ($var) {
if ($var === '') {
return FALSE;
}
return strval((int)$var) === (string)$var;
}

function testIntVariant3 ($var) {
if ($var === '') {
return FALSE;
}
return strval((int)$var) === strval($var);
}

function resultTest ($function, $a) {
return $function($a);
}

function timeTest ($function, $a) {
global $times;
$time_start = microtime(TRUE);
for ($i = 0; $i < $times; $i++) {
$function($a);
}
return (microtime(TRUE) - $time_start);
}

function showResult($result) {
if($result === FALSE) {
return "FALSE";
}
if($result === TRUE) {
return "TRUE ";
}

}
$testcases = array (
'int' => array(32425),
'negative int' => array(-32425),
'largest int' => array(PHP_INT_MAX),
'int as string' => array('32425'),
'negative int as string' => array('-32425'),
'zero' => array(0),
'zero as string' => array('0'),
'int as string with leading zero' => array('01234'),
'positive int as string with plus modifier' => array('+1234'),
'negative int as string with leading zero' => array('-01234'),
'largest int plus one' => array(PHP_INT_MAX + 1),
'string' => array('testInt'),
'empty string' => array(''),
'int in string' => array('5 times of testInt'),
'int as string with space after' => array('5 '),
'int as string with space before' => array(' 5'),
'int as string with many spaces before' => array(' 5'),
'float' => array(3.14159),
'float as string' => array('3.14159'),
'float as string only a dot' => array('10.'),
'float as string trailing zero would evaluate to int 10' => array('10.0'),
'float as string trailing zeros would evaluate to int 10' => array('10.00'),
'null' => array(NULL),
);
?>
<pre>
<?php
foreach ($testcases as $name => $parameters) {
echo "\n$name";

$baseTime = timeTest ('testInt', $parameters[0]);
$result = showResult(resultTest ('testInt', $parameters[0]));
echo "\noriginal $result : " . sprintf("%.3f", $baseTime);

$time = timeTest ('testIntVariantTrunk', $parameters[0]);
$result = showResult(resultTest ('testIntVariantTrunk', $parameters[0]));
echo "\nvariantTrunk $result : " . sprintf("%.3f", $time) . ' gain ' . number_format((($baseTime/$time)*100) - 100, 1) . ' %';

$time = timeTest ('testIntVariant1', $parameters[0]);
$result = showResult(resultTest ('testIntVariant1', $parameters[0]));
echo "\nvariant1 $result : " . sprintf("%.3f", $time) . ' gain ' . number_format((($baseTime/$time)*100) - 100, 1) . ' %';

$time = timeTest ('testIntVariant2', $parameters[0]);
$result = showResult(resultTest ('testIntVariant2', $parameters[0]));
echo "\nvariant2 $result : " . sprintf("%.3f", $time) . ' gain ' . number_format((($baseTime/$time)*100) - 100, 1) . ' %';

$time = timeTest ('testIntVariant3', $parameters[0]);
$result = showResult(resultTest ('testIntVariant3', $parameters[0]));
echo "\nvariant3 $result : " . sprintf("%.3f", $time) . ' gain ' . number_format((($baseTime/$time)*100) - 100, 1) . ' %';


echo "\n";
}

?>

</pre>
(2-2/2)