Req #63655 [Opn->Ana]: check type functions -> check type operators
| From: | kalle@php.net | Date: | Sat, 06 Aug 2016 02:50:55 +0000 |
| Subject: | Req #63655 [Opn->Ana]: check type functions -> check type operators | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-202985@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=63655&edit=1
ID: 63655
Updated by: kalle@php.net
Reported by: sailormax at inbox dot lv
Summary: check type functions -> check type operators
-Status: Open
+Status: Analyzed
Type: Feature/Change Request
Package: Performance problem
PHP Version: 5.3.19
Block user comment: N
Private report: N
New Comment:
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) { }
Previous Comments:
------------------------------------------------------------------------
[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