(I'm leaving the original answer intact for posterity, but please read the edit at the bottom of the answer as the answer is wrong).
Why create a complicated line of code, such as:
array_combine(range(date('Y'), date('Y')+10), range(date('Y'), date('Y')+10))
When you can do a simple for-loop, such as:
$current_year = date('Y');
$max_year = $current_year + 10;
for($i = $current_year; $i <= $max_year; $i++){
$year_array[$i] = $i;
}
Well - speed. The one liner mixture of array_combine()
, range()
and date()
is multiple times faster than the for-loop. Not to mention that it creates less work for the GC.
I ran the two pieces of code above and this was the output:
$start_0 = microtime();
$year_array = array_combine(range(date('Y'), date('Y')+10), range(date('Y'), date('Y')+10));
$end_0 = microtime();
$start_1 = microtime(true);
$current_year = date('Y');
$max_year = $current_year + 10;
$year_array = array();
for($i = $current_year; $i <= $max_year; $i++){
$year_array[$i] = $i;
}
$end_1 = microtime(true);
echo "<pre>";
print_r($year_array);
echo "0 Time: " . ($end_0 - $start_0) . "<br>";
echo "1 Time: " . ($end_1 - $start_1) . "<br>";
echo "</pre>";

Now, even if we make use of inlining the function calls, it gets even worse! See:
$start_0 = microtime();
$year_array = array_combine(range(date('Y'), date('Y')+10), range(date('Y'), date('Y')+10));
$end_0 = microtime();
$start_1 = microtime(true);
$year_array = array();
for($i = date('Y'); $i <= date('Y') + 10; $i++){
$year_array[$i] = $i;
}
$end_1 = microtime(true);
echo "<pre>";
print_r($year_array);
echo "0 Time: " . ($end_0 - $start_0) . "<br>";
echo "1 Time: " . ($end_1 - $start_1) . "<br>";
echo "</pre>";

So, the best option is to go the array_combine(), range(), date()
route. However, we can further optimize it as:
$start_range = date('Y');
$range = range($start_range, $start_range + 10)
array_combine($range, $range)
That way, we end up with 2 function calls to date(), range()
and 2 new variables (which are cheaper than a function call), but is still not as performant as your original one liner.
$start_0 = microtime();
$year_array = array_combine(range(date('Y'), date('Y')+10), range(date('Y'),, date('Y')+10));
$end_0 = microtime();
$start_1 = microtime(true);
$year_array = array();
for($i = date('Y'); $i <= date('Y') + 10; $i++){
$year_array[$i] = $i;
}
$end_1 = microtime(true);
$start_2 = microtime(true);
$start_range = date('Y');
$range = range($start_range, $start_range + 10);
array_combine($range, $range);
$end_2 = microtime(true);
echo "<pre>";
print_r($year_array);
echo "0 Time: " . ($end_0 - $start_0) . "<br>";
echo "1 Time: " . ($end_1 - $start_1) . "<br>";
echo "2 Time: " . ($end_2 - $start_2) . "<br>";
echo "</pre>";

My recommendation would be to keep your one liner.
EDIT: My recommendation is you do #2 as I missed microtime(true) on $start_0
and $end_0
, thus I completely misread the benchmark output (which would make the one liner the slowest of the three tests). Thank you for catching that @Brett Santore.