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.
time required (shorter is better)
rank / method / description / code
resulting output
<?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
<?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
<?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
<?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
<?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