View Single Post
Old 11-03-2007, 02:49 PM   #4 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 676
DJMaze is on a distinguished road
Funny, i've just tested something with a fellow coder.
It seems using ternary operators in PHP is slower then binary if {} else {}

PHP Code:
<?php

$time 
microtime(true);
for (
$i=0$i<1000000; ++$i)
{
    
$foo = ($i%2==0) ? 1;
}
$time microtime(true)-$time;
echo 
'ternary in: '.round($time,4).' seconds<br>';

$time microtime(true);
for (
$i=0$i<1000000; ++$i)
{
    if (
$i%2==0)
    {
        
$foo 0;
    }
    else
    {
        
$foo 1;
    }
}
$time microtime(true)-$time;
echo 
'if..else.. in: '.round($time,4).' seconds<br>';
Result P4 2.54
Code:
ternary in: 0.5543 seconds
if..else.. in: 0.5709 seconds
Result P2
Code:
ternary in: 3.4309 seconds
if..else.. in: 2.7495 seconds
It's hardly noticeable though since how often do you use the ternary?
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote