views:

174

answers:

3

I am trying to figure out the fastest way (in PHP 5) to check that a value is the type I need it to be. I created two lines of code which both do the same thing. The problem is that I can't figure out which is fastest based off of benchmarks.

(is_scalar($value) ? intval($value) : 0);
settype($value, 'integer');

I created the following test code but I don't have any more than my own PC (Core2Quad + XP 32bit + php5.2.5) and a dreamhost account to test it with - both of which show about the same times for this code.

$array = array(
    'false' => FALSE,
    'false2'=> 0,
    'false3'=> '0',
    'false4'=> 'FALSE',
    'true' => TRUE,
    'true2' => 1,
    'true3' => '1',
    'true4' => 'TRUE',

    'char' => chr(250),
    'char2' => chr(10),
    'utf' => 0xF0,
    'utf1' => 0xFE,

    'number' => '452.5435',
    'number2' => '-3948.33e2',
    'number3' => -343.54,
    'number4' => 99.999,
    'number5' => '3jk439fjk23945t324098523.349fj324r',

    'int' => 2323,
    'int2' => '2345',
    'int3' => '0',
    'int4' => array(),
    'int5' => '39582347823908270983249078530793249802357846t890234879023490785',
    'int6' => 3895732890543789324890123467548093248976123890548793289073246789458901234,

    'object3' => new SimpleXMLElement('<xml></xml>'),

    'array' => array(),
    'array2' => array('hello'),
    'array3' => array(3,54,21,0),
    'array4' => array(0.2)
);


$start = microtime(TRUE);

for($x=0;$x<10000;$x++) {
    foreach( $array as $value ) {
     (is_scalar($value) ? intval($value) : 0);
     //settype($value, 'integer');
    }
}

print (microtime(TRUE) - $start). ' seconds';

Anyway, I was wondering if there might be more here that I am missing as to which of these methods might not only work faster - but might yield odd results as well. Another thing is that should this prove success full with ints - then other types such as strings and floats should also work.

:UPDATE:

I just tested these methods against the float type and found that settype() was slower (.28 sec) vs floatval() (.21 sec). So the answer to this question may only be valid for the int type.

//Float
(is_scalar($value) ? floatval($value) : 0);
settype($value, 'float');
A: 

Unless you're planning on testing a bazillion values, there should not be any practical speed difference. Any that exists is so small it doesn't really affect anything.

Jani Hartikainen
What about the second part of the question? Any "odd results" with one over the other?
Xeoncross
+2  A: 

i guess you're asking out of pure theoretical interest, because the speed differences in this particular case cannot be considered important in practice.

let's take a look into php source code

intval http://lxr.php.net/source/php-src/ext/standard/type.c#142

settype http://lxr.php.net/source/php-src/ext/standard/type.c#95

as you can see, both function use the same convert_to_long routine (which in turn reduces to the library call strtol). settype includes (a tiny) overhead of comparing the second argument with a type string, so it should be slightly slower.

the fastest method would be to use (int) cast, because it doesn't involve a function call opcode, but executed directly by VM.

stereofrog
Thanks, I always forget that buried deep in the PHP site is the source code repo. The only problem is that this is the PHP 6 source - I wonder if PHP 5 has the same code.
Xeoncross
i don't think this code was changed much since php3 ;)
stereofrog
Well, since settype() didn't come out until PHP 4... ;)
Xeoncross
A: 

Stereofrog is right that direct (type) casting is the fastest. Here is the code I use now.

(is_scalar($int) && is_numeric($int) ? (int) $int : 0)
Xeoncross