ID:               48828
 Updated by:       ras...@php.net
 Reported By:      php at richardneill dot org
-Status:           Open
+Status:           Feedback
 Bug Type:         Performance problem
 Operating System: Linux
 PHP Version:      6CVS-2009-07-07 (CVS)
 New Comment:

Have you set your timezone in your php.ini?  I don't see it set in your
script.  And is this actually PHP6?


Previous Comments:
------------------------------------------------------------------------

[2009-07-07 05:05:12] php at richardneill dot org

Description:
------------
I've just been troubleshooting a script whose job is to fix 
logfiles. Each line of the logfile has a timestamp, but only
HH:MM:SS and it's missing the date. My script has to fix this.

For a 500,000 line file, this takes about 2 minutes, so I 
investigated further. I found that strtotime() was really quite a 
lot slower than I expected, and that using preg_match() instead 
doubled the script execution speed.

Is this really to be expected, or could strtotime() be optimised?


Reproduce code:
---------------
#!/usr/bin/php
<?

//This demonstrates that strtotime() is really rather slow. The 
//example is quite contrived, but surely it should be possible to 
//make strtotime() much faster than an entire regular-expression
//match, rather than 3 times slower?

$time1=microtime(1);

$timestamp1="05:03:02";
$timestamp2="03:05:07";

for ($i=0;$i<1E6;$i++){
        $seconds = strtotime($timestamp1)  - strtotime($timestamp2);
        #echo "$seconds\n"; //Do something with $seconds
}

$time2=microtime(1);

for ($i=0;$i<1E6;$i++){
        //preg_match lets us both check that this IS a valid
        //timestamp and split it into parts.
        preg_match('/^(\d\d):(\d\d):(\d\d)(\.\d+)?$/', $timestamp1,
$matches1);
        preg_match('/^(\d\d):(\d\d):(\d\d)(\.\d+)?$/', $timestamp2,
$matches2);

        $seconds = ( 3600 * intval($matches1[1]) + 60 *
        intval($matches1[2]) + intval($matches1[3]) ) -
        ( 3600 * intval($matches2[1]) + 60 * 
        intval($matches2[2]) + intval($matches2[3])  );
        #echo "$seconds\n";  //Do something with $seconds
}

$time3=microtime(1);

# 42.8 on 2.4GHz CPU
echo "Each pair of calls to strtotime() took:".($time2 - $time1 )."
microseconds \n";
   
# 16.6.
echo "Each pair of calls to preg_match() etc took:".($time3 - $time2
)." microseconds \n";
?>

Expected result:
----------------
I expect strtotime(), which is designed specifically for the 
purpose, to be faster than preg_match, which is more 
general-purpose.

Actual result:
--------------
strtotime() takes about 3 x longer than the brute-force approach 
using a regular-expression + array maths. Is that really expected?
date() isn't very fast either.

42us means 100,000 CPU cycles!



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=48828&edit=1

Reply via email to