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.

remove array item by value

methods sorted by order executed

jump to method

time required (shorter is better)

methods sorted by performance

rank / method / description / code

resulting output

1. explode / join
  • 0.000847 seconds for 15 repeats
  • 1.3 x faster than array walk
  • only works if the delimiter (in this case ",") is not in any array value



<?php

// ------- remove array item by value with explode / join

// -- the setup

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

$item2remove 'c';


// -- the code to test

$newworkdata explode(',',str_replace($item2remove.',','',(join(',',$workdata))));


?>
Array
(
    [0] => a
    [1] => b
    [2] => d
    [3] => e
    [4] => f
    [5] => g
    [6] => h
    [7] => i
    [8] => j
    [9] => k
    [10] => l
    [11] => m
    [12] => n
    [13] => o
    [14] => p
    [15] => q
    [16] => r
    [17] => s
    [18] => t
    [19] => u
    [20] => v
    [21] => w
    [22] => x
    [23] => y
    [24] => z
)
back to the PHP benchmark results
2. foreach loop
  • 0.001103 seconds for 15 repeats
  • 0.8 x faster than array walk



<?php

// ------- remove array item by value with foreach loop

// -- the setup

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

$item2remove 'c';


// -- the code to test

foreach($workdata as $item)
{
    if(
$item != $item2remove$newworkdata[] = $item;
}


?>
Array
(
    [0] => a
    [1] => b
    [2] => d
    [3] => e
    [4] => f
    [5] => g
    [6] => h
    [7] => i
    [8] => j
    [9] => k
    [10] => l
    [11] => m
    [12] => n
    [13] => o
    [14] => p
    [15] => q
    [16] => r
    [17] => s
    [18] => t
    [19] => u
    [20] => v
    [21] => w
    [22] => x
    [23] => y
    [24] => z
)
back to the PHP benchmark results
3. fast for loop
  • 0.001526 seconds for 15 repeats
  • 0.3 x faster than array walk



<?php

// ------- remove array item by value with fast for loop

// -- the setup

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

$item2remove 'c';


// -- the code to test

$n count($workdata);
for(
$i 0$i $n$workdata[$i] != $item2remove$i++)
{
    
$newworkdata[] = $workdata[$i];
}


?>
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
    [6] => g
    [7] => h
    [8] => i
    [9] => j
    [10] => k
    [11] => l
    [12] => m
    [13] => n
    [14] => o
    [15] => p
    [16] => q
    [17] => r
    [18] => s
    [19] => t
    [20] => u
    [21] => v
    [22] => w
    [23] => x
    [24] => y
    [25] => z
)
back to the PHP benchmark results
4. array filter
  • 0.001527 seconds for 15 repeats
  • 0.3 x faster than array walk
  • this code requires extra code
  • this code retains the key => value



<?php

// ------- remove array item by value with array filter

// -- the setup

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

$item2remove 'c';


// -- extra code

function istrue($elem)
{
    global 
$item2remove;
    if(
$elem != $item2remove) return true;
    else return 
false;
}


// -- the code to test

$newworkdata array_filter($workdata'istrue');


?>
Array
(
    [0] => a
    [1] => b
    [3] => d
    [4] => e
    [5] => f
    [6] => g
    [7] => h
    [8] => i
    [9] => j
    [10] => k
    [11] => l
    [12] => m
    [13] => n
    [14] => o
    [15] => p
    [16] => q
    [17] => r
    [18] => s
    [19] => t
    [20] => u
    [21] => v
    [22] => w
    [23] => x
    [24] => y
    [25] => z
)
back to the PHP benchmark results
5. for loop
  • 0.001624 seconds for 15 repeats
  • 0.2 x faster than array walk



<?php

// ------- remove array item by value with for loop

// -- the setup

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

$item2remove 'c';


// -- the code to test

$n count($workdata);
for(
$i 0$i $n$i++)
{
    if(
$workdata[$i] != $item2remove$newworkdata[] = $workdata[$i];
}


?>
Array
(
    [0] => a
    [1] => b
    [2] => d
    [3] => e
    [4] => f
    [5] => g
    [6] => h
    [7] => i
    [8] => j
    [9] => k
    [10] => l
    [11] => m
    [12] => n
    [13] => o
    [14] => p
    [15] => q
    [16] => r
    [17] => s
    [18] => t
    [19] => u
    [20] => v
    [21] => w
    [22] => x
    [23] => y
    [24] => z
)
back to the PHP benchmark results
6. array walk
  • 0.001951 seconds for 15 repeats
  • slowest method
  • this code requires extra code



<?php

// ------- remove array item by value with array walk

// -- the setup

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

$item2remove 'c';


// -- extra code

function addelement($elem)
{
    global 
$newworkdata$item2remove;
    if(
$elem != $item2remove$newworkdata[] = $elem;
}


// -- the code to test

array_walk($workdata'addelement');


?>
Array
(
    [0] => a
    [1] => b
    [2] => d
    [3] => e
    [4] => f
    [5] => g
    [6] => h
    [7] => i
    [8] => j
    [9] => k
    [10] => l
    [11] => m
    [12] => n
    [13] => o
    [14] => p
    [15] => q
    [16] => r
    [17] => s
    [18] => t
    [19] => u
    [20] => v
    [21] => w
    [22] => x
    [23] => y
    [24] => z
)
back to the PHP benchmark results