Fastest PHP Type Jugling with settype() vs *val() functions?
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');
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.
Let's take a simple benchmark:
<?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 64bit Ubuntu:
intval: 0.47533583641052 sec.
(int): 0.19618892669678 sec.
settype: 0.51951289176941 sec.
On long integers the picture is similar. So (int) is the best, but there's no real reason to use intval() instead of settype()
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.
Direct (type) casting is the fastest. Here is the code I use now.
(is_scalar($int) && is_numeric($int) ? (int) $int : 0)
精彩评论