Advertisement:

View Issue Details Jump to Notes ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0003743SMF 2.1[SMF 3.0 "Saguaro"] public2009-09-10 16:152014-01-25 13:30
ReporterMark Rose 
PrioritynormalSeveritytweakReproducibilityN/A
StatusclosedResolutionwon't fix 
PlatformOSOS Version
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.
Tagsperformance
Attached Files? file icon speedtest.php [^] (349 bytes) 2009-09-10 18:20

- Relationships

-  Notes
(0009642)
karlbenson (SMF Friend)
2009-09-10 16:35

Drupal made this change already

Well they committed the change
http://drupal.org/node/216357 [^]
but forgot about php4
http://drupal.org/node/367477 [^]

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

define('REQUEST_TIME', empty($_SERVER['REQUEST_TIME']) ? time() : $_SERVER['REQUEST_TIME']);
or
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)
or
$context['request_time'] = empty($_SERVER['REQUEST_TIME']) ? time() : $_SERVER['REQUEST_TIME']
(0009643)
Thantos (SMF Friend)
2009-09-10 18:14

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.
(0009644)
Thantos (SMF Friend)
2009-09-10 18:23

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.
(0009645)
Mark Rose (Beta Tester)
2009-09-10 23:50
edited on: 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.

(0013660)
groundup (SMF Friend)
2011-02-05 01:59

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.
(0014943)
arrowtotheknee (Developer)
2014-01-25 13:30

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 1.2.8 (Modified)[^] Copyright © 2000 - 2010 Mantis Group