r75429 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75428‎ | r75429 | r75430 >
Date:15:14, 26 October 2010
Author:platonides
Status:ok
Tags:
Comment:
Cache the results of wfIsWindows()
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";
Modified paths:
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -2068,11 +2068,11 @@
20692069 * @return Bool: true if it's Windows, False otherwise.
20702070 */
20712071 function wfIsWindows() {
2072 - if ( substr( php_uname(), 0, 7 ) == 'Windows' ) {
2073 - return true;
2074 - } else {
2075 - return false;
 2072+ static $isWindows = null;
 2073+ if ( $isWindows === null ) {
 2074+ $isWindows = substr( php_uname(), 0, 7 ) == 'Windows';
20762075 }
 2076+ return $isWindows;
20772077 }
20782078
20792079 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r75446Follow up r75429 : benchmark for wfIsWindows();...hashar17:21, 26 October 2010

Status & tagging log