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.

convert array values to uppercase

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.001012 seconds for 15 repeats
  • 0.6 x faster than array walk
  • only works if the delimiter (in this case ",") is not in any array value



<?php

// ------- convert array values to uppercase with explode / join

// -- the setup

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


// -- the code to test

$newworkdata explode(',',strtoupper(join(',',$workdata)));


?>
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
2. foreach loop
  • 0.001058 seconds for 15 repeats
  • 0.5 x faster than array walk



<?php

// ------- convert array values to uppercase with foreach loop

// -- the setup

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


// -- the code to test

foreach($workdata as $item)
{
    
$newworkdata[] = strtoupper($item);
}


?>
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
3. do while loop
  • 0.001311 seconds for 15 repeats
  • 0.2 x faster than array walk



<?php

// ------- convert array values to uppercase with do while loop

// -- the setup

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


// -- the code to test

$i 0;
do
{
    
$newworkdata[] = strtoupper($workdata[$i++]);
} while(isset(
$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. for loop
  • 0.001557 seconds for 15 repeats
  • 0.1 x faster than array walk



<?php

// ------- convert array values to uppercase with for loop

// -- the setup

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


// -- the code to test

$n count($workdata);
for(
$i 0$i $n$i++)
{
    
$newworkdata[] = strtoupper($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
5. array walk
  • 0.001635 seconds for 15 repeats
  • slowest method
  • this code requires extra code



<?php

// ------- convert array values to uppercase with array walk

// -- the setup

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


// -- extra code

function str2upper($elem)
{
    global 
$newworkdata;
    
$newworkdata[] = strtoupper($elem);
}


// -- the code to test

array_walk($workdata'str2upper');


?>
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