Performance Comparison: Array Functions vs. Foreach Loop
Explore the performance differences between PHP's array_map(), array_filter(), and array_reduce() functions versus traditional foreach loops.
edit_note
PHP Code Editor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
echo '<pre>';
// Generate a large array for testing
$largeArray = range(1, 100000);
// Benchmark array_map
$startMap = microtime(true);
$resultMap = array_map(function($value) {
return $value * 2; // Example operation
}, $largeArray);
$endMap = microtime(true);
$mapTime = $endMap - $startMap;
// Benchmark array_filter
$startFilter = microtime(true);
$resultFilter = array_filter($largeArray, function($value) {
return $value % 2 === 0; // Example: filter even numbers
});
$endFilter = microtime(true);
$filterTime = $endFilter - $startFilter;
// Benchmark array_reduce
$startReduce = microtime(true);
$resultReduce = array_reduce($largeArray, function($carry, $item) {
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
settingsPHP Version
task_alt
7.2
panorama_fish_eye
7.4
panorama_fish_eye
8.0
panorama_fish_eye
8.1
panorama_fish_eye
8.2
panorama_fish_eye
8.3
panorama_fish_eye
8.4
panorama_fish_eye
8.5
terminal
Execution Result
play_circle_outline
Ready to execute
Click the "Run Script" button to see the output here
article
Description
Performance Comparison: Array Functions vs. Foreach Loop in PHP
analyze the performance of some commonly used PHP array functions — array_map(), array_filter(), and array_reduce() — in comparison to traditional loops using foreach. Understanding these differences can help you choose the most efficient approach for your PHP applications.
Overview of Array Functions
- array_map(): Applies a callback function to each element of the given array and returns a new array.
- array_filter(): Filters elements of an array using a callback function, returning only those that pass the test.
- array_reduce(): Iteratively reduces an array to a single value using a callback function.
Comparing array_map(), array_filter(), and array_reduce() with traditional loops or foreach helps understand their performance implications and readability. Below is a performance test example, along with explanations for each method.
Benchmarking Script
We created a benchmarking test to evaluate performance using a large array of one million elements. Here’s the script used for the test:
Comments
No comments yet
Be the first to share your thoughts!