php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40165 define() cost
Submitted: 2007-01-19 11:28 UTC Modified: 2007-01-20 15:45 UTC
From: gacek at intertele dot pl Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 5.2.0 OS: Linux FC4 x86_64
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: gacek at intertele dot pl
New email:
PHP Version: OS:

 

 [2007-01-19 11:28 UTC] gacek at intertele dot pl
Description:
------------
When playing with apd profiler, I have noticed huge cost of define() call. It's over 20x the cost of average count() call.
Might it be apd inaccuracy or define() specifics, or define() implementation is heavily broken?


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-19 11:33 UTC] gacek at intertele dot pl
This is sample of "pprofp -u" output from fairly complex app:


Total Elapsed Time = 2.47
Total System Time  = 0.35
Total User Time    = 1.36


         Real         User        System             secs/    cumm
%Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name
--------------------------------------------------------------------------------------
13.9 0.24 0.24  0.19 0.19  0.04 0.04   850  0.0002   0.0002            0 define
12.1 0.27 0.27  0.16 0.16  0.05 0.05  18653  0.0000   0.0000            0 count
10.9 0.15 0.15  0.15 0.15  0.04 0.04  11652  0.0000   0.0000            0 is_array
9.1 0.26 0.26  0.12 0.12  0.03 0.03  15489  0.0000   0.0000            0 each
6.2 0.18 0.18  0.08 0.08  0.02 0.02  9012  0.0000   0.0000            0 array_shift
4.1 0.08 0.08  0.06 0.06  0.02 0.02  5402  0.0000   0.0000            0 ereg_replace
4.1 0.04 0.04  0.06 0.06  0.01 0.01   655  0.0001   0.0001            0 mysql_fetch_array
3.8 0.07 0.07  0.05 0.05  0.01 0.01  5277  0.0000   0.0000            0 urlencode
2.9 0.04 0.04  0.04 0.04  0.00 0.00  2520  0.0000   0.0000            0 func_num_args
2.9 0.05 0.05  0.04 0.04  0.00 0.00  1465  0.0000   0.0000            0 sprintf
 [2007-01-19 11:33 UTC] tony2001@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.



 [2007-01-19 14:19 UTC] tony2001@php.net
count(array()) just returns ht->nNumOfElements, this is probably the most simple function in PHP.
define() copies (malloc() + memcpy()) the value, duplicates the name (malloc() + memcpy() again) and adds it to the hash of constants (which might involve realloc() etc.).

So it's kinda expected that a function which does almost nothing is a bit faster, like 7x or 6x, which are the numbers I can see myself.
 [2007-01-19 23:39 UTC] gacek at intertele dot pl
OK, comparison with count() was probably bad, but I will give you another example:
I have declared 3 functions named myf1, myf2, myf3, which play with hash table. Not big one (10-20 elements), but processing is a bit complex. apd profiler shows following call tree of myf1():

                myf1
                  func_num_args
                  func_num_args
                  func_get_args
                  myf2
                    count
                    array_shift
                    is_array
                    reset
                    each
                    each
                    each
                    count
                    array_shift
                    array_shift
                    is_array
                    reset
                    each
                    in_array
                    each
                    in_array
                    each
                    count
                    array_shift
                    is_array
                    array_shift
                    count
                    array_shift
                    is_array
                    array_shift
                    count
                    myf3
                      each
                      is_array
                      each
                      is_array
                      each
                      is_array
                      each
                  is_array
                  count
                  urlencode
                  array_push
                  count
                  urlencode
                  array_push
                  count
                  reset
                  ksort
                  each
                  is_array
                  ereg_replace
                  ereg_replace
                  ereg_replace
                  ereg_replace
                  urlencode
                  urlencode
                  array_push
                  each
                  join

Each call of myf1() does nearly the same and operates on similiar hash. As you can see, above code envolves really complex operations as urlenconde'ing and ereg_replace'ing.
Now, let's take a look at sample "pprofp -U" output. I have taken only relevant rows from it:

Total Elapsed Time = 0.40
Total System Time  = 0.04
Total User Time    = 0.35


         Real         User        System             secs/  cumm
%Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name
--------------------------------------------------------------------------------------
100.0 0.00 0.40  0.00 0.35  0.00 0.04     1  0.0000   0.3480            0 main
56.3 0.22 0.22  0.20 0.20  0.03 0.03   850  0.0002   0.0002            0 define
5.7 0.00 0.03  0.00 0.02  0.00 0.00    43  0.0000   0.0005            0 myf1
3.4 0.00 0.02  0.00 0.01  0.00 0.00    44  0.0000   0.0003            0 myf2
2.3 0.00 0.00  0.00 0.01  0.00 0.00    44  0.0000   0.0002            0 myf3


So, 850 calls of define() took cumulative 0.20s of user time from overall 0.35s (57%). 44 calls of complex myf1() took cumulatve 0.02s of user time (5.7%).
It looks like average constant definition takes near half of operations from myf1() complex call tree 8-/.
I would really classify "define" as too expensive.
 [2007-01-20 15:45 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Has to do with the size of the constants hash table.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC