lixlpixel PHP Benchmarks

there are various common tasks in PHP which can be done in several ways.
this site aims to benchmark these different approaches to find the fastest solution to the problem.

sometimes there are builtin PHP functions which are (in the most cases) the fastest solutions.
but their options are limited so there might be some benchmarks which don't make sense to you at first.

check if a string is in the values of an array

methods sorted by order executed

jump to method

time required (shorter is better)

methods sorted by performance

rank / method / description / code

resulting output

1. in array
  • 0.00017 seconds for 15 repeats
  • 2 x faster than for loop / strpos
  • this method only works if a value is exactly the string to search for



<?php

// ------- check if a string is in the values of an array with in array

// -- the setup

$workdata range('a','z');

$item2find 'u';


// -- the code to test

$newworkdata 'string '.(in_array($item2find,$workdata) ? '' 'not').' found';


?>
string  found
back to the PHP benchmark results
2. join / strpos
  • 0.000267 seconds for 15 repeats
  • 0.9 x faster than for loop / strpos



<?php

// ------- check if a string is in the values of an array with join / strpos

// -- the setup

$workdata range('a','z');

$item2find 'u';


// -- the code to test

if(strpos(join(' ',$workdata),$item2find) !== false )
{
    
$newworkdata 'string found';
}else{
    
$newworkdata 'string not found';
}


?>
string found
back to the PHP benchmark results
3. for loop / strpos
  • 0.000517 seconds for 15 repeats
  • slowest method
  • this code is faster the earlier in the array the string is found



<?php

// ------- check if a string is in the values of an array with for loop / strpos

// -- the setup

$workdata range('a','z');

$item2find 'u';


// -- the code to test

$n count($workdata);
for(
$i 0$i $n$i++)
{
    if(
strpos($workdata[$i],$item2find) !== false)
    {
        
$newworkdata 'string found';
        
$i $n;
    }else{
        
$newworkdata 'string not found';
    }
}


?>
string found
back to the PHP benchmark results