php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63655 check type functions -> check type operators
Submitted: 2012-11-30 11:11 UTC Modified: 2016-08-06 09:39 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: sailormax at inbox dot lv Assigned: nikic (profile)
Status: Closed Package: Performance problem
PHP Version: 5.3.19 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: sailormax at inbox dot lv
New email:
PHP Version: OS:

 

 [2012-11-30 11:11 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-02 13:01 UTC] 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.
 [2014-03-16 01:56 UTC] 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
 [2016-08-06 02:50 UTC] kalle@php.net
-Status: Open +Status: Analyzed
 [2016-08-06 02:50 UTC] 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) { }
 [2016-08-06 09:39 UTC] nikic@php.net
-Status: Analyzed +Status: Closed -Assigned To: +Assigned To: nikic
 [2016-08-06 09:39 UTC] nikic@php.net
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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 17:01:34 2025 UTC