Req #63655 [Ana->Csd]: check type functions -> check type operators
Edit report at https://bugs.php.net/bug.php?id=63655&edit=1
ID: 63655
Updated by: nikic@php.net
Reported by: sailormax at inbox dot lv
Summary: check type functions -> check type operators
-Status: Analyzed
+Status: Closed
Type: Feature/Change Request
Package: Performance problem
PHP Version: 5.3.19
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
Type-checking functions are already implemented as VM instructions as of PHP 7 (in namespaced code,
use \is_string), as such there should no longer be any performance concerns.
Previous Comments:
------------------------------------------------------------------------
[2016-08-06 02:50:52] kalle@php.net
I suppose most of these type checking functions could be implemented as special types (much like
strlen() is internally for constant strings). So in case a variable can be checked at compile time,
the call can be optimized away.
// Can be optimized (compile time)
$name = 'Kalle';
if(is_string($name)) { }
// Cannot be optimized (runtime), unless return type hinting is used (but what about NULL?)
$name = getName();
if(is_string($name) { }
------------------------------------------------------------------------
[2014-03-16 01:56:31] jtrumbull at gmail dot com
it would appear cast conversion and comparison is only 2x faster when the result is TRUE, it is 3x
slower when the answer is FALSE
------------------------------------------------------------------------
[2013-01-02 13:01:59] vincent at lycoops dot be
This is true for simple scalars, yes.
But for large strings, array and objects, it is wrong.
But I agree that this should really be added.
------------------------------------------------------------------------
[2012-11-30 11:11:17] sailormax at inbox dot lv
Description:
------------
Currently check type functions (is_string, is_int, is_float, is_bool, is_object, is_array) are
functions. They are very often use in many engines. But because they are functions - they are very
slow for engines.
In result engine authors have to use faster alternatives (~2x faster) like:
is_string => ((string)$var === $var)
is_int => ((int)$var === $var)
is_float => ((float)$var === $var)
is_bool => ((bool)$var === $var)
is_object => ((object)$var === $var)
is_array => ((array)$var === $var)
Can you compile this type of functions as operators?
thank you.
Test script:
---------------
$ts = microtime(true);
for ($i=0; $i<50000; $i++) $res = is_string($_GET);
var_dump(microtime(true) - $ts);
print "<br /><br />";
$ts = microtime(true);
for ($i=0; $i<50000; $i++) $res = ((string)$_GET === $_GET);
var_dump(microtime(true) - $ts);
print "<br /><br />";
Expected result:
----------------
equal time
Actual result:
--------------
is_string ~ 2x slower than ((string)$_GET === $_GET)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63655&edit=1
Thread (5 messages)