php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34700 performance problem with date()
Submitted: 2005-10-01 16:07 UTC Modified: 2005-10-07 23:27 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: six at aegis-corp dot org Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5CVS-2005-10-02 OS: *
Private report: No CVE-ID: None
 [2005-10-01 16:07 UTC] six at aegis-corp dot org
Description:
------------
performance of the date() function has taken a huge hit between PHP 5.0 and 5.1. i believe this is because of an unneeded syscall when a second argument is given.

Reproduce code:
---------------
<?

for ($a = 0; $a < 100000; $a++) date("Y-m-d H:i:s", $a);

?>

Expected result:
----------------
time /usr/local/bin/php-cli-5.0.4 datetest.php    
real 0m2.022s
user 0m1.910s
sys  0m0.000s

Actual result:
--------------
time /usr/local/bin/php-cli-5.1.0RC1 datetest.php 
real 0m10.001s
user 0m8.120s
sys  0m0.130s

5.1.0 takes more than 4x the time to complete than 5.0.4

with an strace, I can see that 5.1.0RC1 makes one (probably unneeded) time() syscall per call to date(), which 5.0.4 does not

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-01 17:13 UTC] rasmus@php.net
One of the new things in PHP 5.1 is the $_SERVER['REQUEST_TIME'] variable that gets set to the request time.  When running under Apache this comes directly from the time syscall Apache makes for every request, so by using this you can actually eliminate any time-related syscalls at the PHP level and still do time stuff in a PHP script.  But yes, it looks like there are a few things that can be cleaned up here.
 [2005-10-01 19:11 UTC] six at aegis-corp dot org
sniper: the latest cvs exhibits the same problem.

rasmus: $_SERVER["REQUEST_TIME"] is a nice addition, but in my case I just want to convert timestamps stored in a db to a readable format, hence no need for the current time at all ...

the following patch address the unneeded call to time(NULL), strace confirms it, but the performance is still not on par with 5.0

--- php5-200510011430/ext/date/php_date.c.orig  2005-10-01 18:16:55.000000000 +0200
+++ php5-200510011430/ext/date/php_date.c       2005-10-01 18:17:48.000000000 +0200
@@ -479,11 +479,23 @@
 {
        char *format;
        int   format_len;
-       time_t  ts = time(NULL);
+       time_t  ts;
        char           *string;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &format, &format_len, &ts) == FAILURE) {
-               RETURN_FALSE;
+       switch (ZEND_NUM_ARGS()) {
+       case 2:
+               if (zend_parse_parameters(2 TSRMLS_CC, "sl", &format, &format_len, &ts) == FAILURE) {
+                       RETURN_FALSE;
+               }
+               break;
+       case 1:
+               if (zend_parse_parameters(1 TSRMLS_CC, "s", &format, &format_len) == FAILURE) {
+                       RETURN_FALSE;
+               }
+               ts = time(NULL);
+               break;
+       default:
+               WRONG_PARAM_COUNT;
        }
 
        string = php_format_date(format, format_len, ts, localtime TSRMLS_CC);
 [2005-10-01 20:14 UTC] rasmus@php.net
Right, the timezone lookup is currently slower.  There needs to be a cache layer in front of it.  It will come eventually.  We know about it.
 [2005-10-01 21:22 UTC] derick@php.net
So that I can't forget this.
 [2005-10-07 23:27 UTC] derick@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

The syscall has been eliminated and the tzcache is in place now in CVS. It is still slower, but not as bad as it was. Also, things improve greatly if you use the date.timezone setting in php.ini to select the timezone (in the format "America/New_York" f.e.) that you are actually in.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 11:01:34 2024 UTC