Cache the results of wfIsWindows()
authorPlatonides <platonides@users.mediawiki.org>
Tue, 26 Oct 2010 15:14:56 +0000 (15:14 +0000)
committerPlatonides <platonides@users.mediawiki.org>
Tue, 26 Oct 2010 15:14:56 +0000 (15:14 +0000)
Each php_uname() call produces a uname syscall.

The cached one is three times faster (3.197545885) which is liklely to be the difference between a php var lookup and a syscall on my system.

== Test script ==
<?php

function wfIsWindows() {
if ( substr( php_uname(), 0, 7 ) == 'Windows' ) {
return true;
} else {
return false;
}
}

function wfIsWindowsCached() {
static $isWindows = null;
if ( $isWindows === null ) {
$isWindows = substr( php_uname(), 0, 7 ) == 'Windows';
}
return $isWindows;
}

$win = $nonwin = 0;

$time = microtime( true );
for ( $i = 1; $i < 5e8; $i++ ) {
if ( wfIsWindowsCached() ) {
$win++;
} else {
$nonwin++;
}
}

$time = microtime( true ) - $time;
echo "Time elapsed: $time\n";

includes/GlobalFunctions.php

index d047922..acbde8f 100644 (file)
@@ -2068,11 +2068,11 @@ function wfTimestampOrNull( $outputtype = TS_UNIX, $ts = null ) {
  * @return Bool: true if it's Windows, False otherwise.
  */
 function wfIsWindows() {
-       if ( substr( php_uname(), 0, 7 ) == 'Windows' ) {
-               return true;
-       } else {
-               return false;
+       static $isWindows = null;
+       if ( $isWindows === null ) {
+               $isWindows = substr( php_uname(), 0, 7 ) == 'Windows';
        }
+       return $isWindows;
 }
 
 /**