php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46926 mktime() is slow when it gets very small 3rd argument
Submitted: 2008-12-22 13:55 UTC Modified: 2009-06-25 04:33 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: for-bugs at hnw dot jp Assigned: derick (profile)
Status: Not a bug Package: Performance problem
PHP Version: 5.2.8 OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: for-bugs at hnw dot jp
New email:
PHP Version: OS:

 

 [2008-12-22 13:55 UTC] for-bugs at hnw dot jp
Description:
------------
when it gots very small 3rd argument, mktime() performs 1000 times slower than ordinary case.


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

$mt0 = microtime(true);
for($i=0;$i<10;$i++){
  $unixtime = mktime(9,0,-2147483647,1,1,1970);
}
$mt1 = microtime(true);
for($i=0;$i<10;$i++){
  $unixtime = mktime(9,0,2147483646,1,1,1970);
}
$mt2 = microtime(true);
printf("%.6f\n%.6f\n", $mt1-$mt0, $mt2-$mt1);

// This will output
// 5.139942
// 0.000707
// on my machine

Actual result:
--------------
see reproduce code

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-22 14:01 UTC] for-bugs at hnw dot jp
I'm sorry, reproduce code depends on my timezone. Replace code as below:

  $unixtime = mktime(9,0,-2147483647,1,1,1970);

to

  $unixtime = gmmktime(0,0,-2147483647,1,1,1970);

and

  $unixtime = mktime(9,0,2147483646,1,1,1970);

to

  $unixtime = gmmktime(0,0,2147483646,1,1,1970);
 [2009-03-10 18:55 UTC] mmcnicklebugs at googlemail dot com
[PATCH]

I've created a patch [1] which resolves the performance issue. 

do_range_limit() did a na?ve factoring approach when passed a negative offset. I've implemented the same factoring that is used for positive values. All the tests pass.

The patch is against CVS 5.3.

[1] http://whompbox.com/patches/php/tm2unixtime.patch

-- Martin McNickle
 [2009-03-10 22:41 UTC] scottmac@php.net
Duplicate of bug #45822
 [2009-03-22 00:15 UTC] for-bugs at hnw dot jp
This bug is not same as #45822.

This report explains the problem about the performece of mktime(), while #45822 is about endless loop in datetime parser.

Martin's patch looks like good, but is not perfect. There are still slow examples. Such as:

<?php gmmktime(0,0,0,1-12*(int)(2147483647/12),1,1970+(int)(2147483647/12));
 [2009-06-25 04:33 UTC] for-bugs at hnw dot jp
I noticed this is fixed on PHP 5.2.10. Thanks a lot.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC