PHP Data Optimisation / PHP Function Optimisation

Using the keyword generator code from the previous post, i have written a small example to benchmarking script to test it setting up the array in get_filter_words with 2200 keywords:

The test code is as follows:

$text = strip_tags(file_get_contents('http://www.bizzeh.com/'));

for($x=0; $x<3; $x++) {
  $start = microtime(true);
  for($y=0;$y<100; $y++) {
    $ar = get_valid_keywords($text);
  }
  $end = microtime(true);

  echo($end-$start . "<br/>");
}

Using the default get_filter_words function we get average execution times of:

33.2730691433

I decided to optimise this function slightly to improve performance:

function get_filter_words() {
  static $words;
  if(empty($words)) $words = array('000', ..., 'zwölf' );
  return $words;
}

Defining $words as static allows it to persist across the entire script without being cleared on return, and since we are now checking if its empty and only loading the array if it is empty, we are now saving quite a lot of script time:

9.74925804138

Write a comment