Daniel Petrovic Blog

intval vs (int)

<p>I wanted to compare the performance of the function intval() with the typecasting call (int).</p>

<p>The example code was pretty simple:</p>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[

$val = "42";

$start_time = time();

for ($i = 0; $i < 1000000; $i++) {
intval($val); // 1)
// (int) $val; 2)
}

$end_time = time();

echo 'Execution time = ' . ($end_time - $start_time);

]]></ac:plain-text-body></ac:macro>

<p>The test script was run on AMD Phenom X4 with Centos 6.2 OS and PHP-5.3.8 FastCgi and Nginx server</p>

<p>The result: </p>

<p>Case 1) -> ~ 5 sec<br />
Case 2) -> ~ 0 sec</p>