Anonymous Login
10-24-2016 02:15 PM

View Issue Details Jump to Notes ]
IDProjectCategoryView StatusLast Update
0003743SMF 2.1[SMF 3.0 "Saguaro"] public2014-01-25 13:30
ReporterMark Rose 
StatusclosedResolutionwon't fix 
Summary0003743: Use $_SERVER['REQUEST_TIME'] instead of time()
DescriptionOn PHP 5.1 and later, $_SERVER['REQUEST_TIME'] is set. In cases where we spend time calling the time() function, this is faster, because we don't need to make a function call.

It may be worth updating the code to use $_SERVER['REQUEST_TIME'] if PHP > 5.1 is detected.
Attached Files




karlbenson (SMF Friend)

Drupal made this change already

Well they committed the change
but forgot about php4

I'd either replace all instances of time() with either

define('REQUEST_TIME', empty($_SERVER['REQUEST_TIME']) ? time() : $_SERVER['REQUEST_TIME']);
add it to $smcFunc calls (Mark, do you have issue with reducing all function calls per se, or just a particular issue with time()?, I'm not aware of time() taking more than a few microseconds)
$context['request_time'] = empty($_SERVER['REQUEST_TIME']) ? time() : $_SERVER['REQUEST_TIME']


Thantos (SMF Friend)

The immediate questions are how much would it actually save, can you guarantee its accuracy (ie who is setting it), and are there page loads that will take more then 1 second which needs the exact time to the second?

As far as handling PHP 4:

Use Subs-Compat:

if (!isset($_SERVER['REQUEST_TIME']))
  $_SERVER['REQUEST_TIME'] = time();

Then using the server value like normal.


Thantos (SMF Friend)

Did a quick test (file attached).

10,000 assignments

Using $_SERVER['REQUEST_TIME'] it was about 0.02 to 0.03 seconds
Using time() it was around 0.04 to 0.05 seconds with a few hits in the 0.06 to 0.07 seconds range (note: can't rule out normal OS operations causing the delay).

So in short this will have absolutely no noticeable effect on the speed of the system but does introduce lots of bug introduction points.

IMO much better to focus on actual bottlenecks.


Mark Rose (Beta Tester)

Last edited: 2009-09-10 23:51

Yep, I put it as a tweak as it's just a tweak and not a major performance improvement.

$_SERVER['REQUEST_TIME'] is set by PHP at the beginning of the script execution. time() is calculated each time it is called. So we can basically get the time for free using the variable.

I've been playing with the xhprof lately, and making a few small tweaks in the right places in the code I'm working on at work has added up to a significant difference. I managed to save 0.4ms on the homepage load at work (we called time() approximately 80 times).

The test case here isn't valid. When only looping over a call to time(), the code for the function is never pushed from the L1 cache in the processor. Running xhprof on the topic display page in SMF shows 38 calls to time (1.5% of all function calls) taking 0.214 ms to complete. 32 of those calls came from the forum_time function in Subs.php. It adds up.


groundup (SMF Friend)

Started fixing this in the 2.1 branch with rev 10387. It is going to require a lot of testing since almost every file has changed. I obviously avoided anything to do with timeout protection. I couldn't just do a batch replace.


arrowtotheknee (SMF Friend)

Micro-optimisations that save on the order of a fraction of a millisecond, vs lots of possible bugs... hmm. Think we'll give this one a miss for now, I'm afraid.
MantisBT (Modified for SMF Intergration)[^] Copyright © 2000 - 2010 Mantis Group