Fastest PHP Jugling type with settype () vs * val () functions?

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

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

      

I created the following test code but I have no more than my own PC (Core2Quad + XP 32bit + php5.2.5) and a dreamhost account to test it - both of them show about the same time 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, which of these methods might not only work faster, but might also give odd results. Another thing is that if this proves successful with ints - then other types like strings and floats should work as well.

: UPDATE:

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

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

      

+2


source to share


4 answers


I think you are asking for pure theoretical interest, because the speed differences in this particular case cannot be considered important in practice.

consider the 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 functions use the same convert_to_long routine (which in turn boils down to a library call to strtol). settype includes the (tiny) overhead of comparing the second argument to the type string, so it should be a little slower.

The fastest way is to use (int) cast, because it doesn't include the function call opcode, but is executed directly by the VM.

+4


source


Let's take a simple guideline:

<?php
$y = "45678912";

$time_start = microtime(true);
for ($i=0; $i<500000; $i++) {
    $x = $y;
    $x = intval($x);
}
echo "\nintval: " . (microtime(true) - $time_start) . " sec.";

$time_start = microtime(true);
for ($i=0; $i<500000; $i++) {
    $x = $y;
    $x = (int)$x;
}
echo "\n(int): " . (microtime(true) - $time_start) . " sec.";

$time_start = microtime(true);
for ($i=0; $i<500000; $i++) {
    $x = $y;
    settype($x, 'int');
}
echo "\nsettype: " . (microtime(true) - $time_start) . " sec.\n";

      

My results on 64 bit Ubuntu:



intval: 0.47533583641052 sec.
(int): 0.19618892669678 sec.
settype: 0.51951289176941 sec.

      

In long whole images, the picture is similar. So, (int) is the best, but there is no real reason to use intval () instead of settype ()

+1


source


Unless you plan on testing bazillion values, there should be no practical difference in speed. Anyone that exists is so small that nothing really affects anything.

0


source


Direct (type) casting is the fastest. Here is the code I am using now.

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

      

0


source







All Articles