Skip to content

Instantly share code, notes, and snippets.

@Raffaello
Created December 17, 2018 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raffaello/251c0d2c0fb3b4dffe82dcc96831213e to your computer and use it in GitHub Desktop.
Save Raffaello/251c0d2c0fb3b4dffe82dcc96831213e to your computer and use it in GitHub Desktop.
Benchmarks
<?php
$numbers = \range(0, 100000);
$result = [];
$start = \microtime(true);
foreach ($numbers as $number) {
$result[] = $number * 10;
}
$end = \microtime(true);
echo 'total time foreach: ' . ($end-$start);
echo "\n";
$start = \microtime(true);
$result = \array_map(function($n) { return $n * 10; }, $numbers);
$end = \microtime(true);
echo 'total time map: ' . ($end-$start);
/*
* Results: PHP5.6 (related to V1 environment)
+ total time foreach: 0.26890397071838
- total time map: 1.4822571277618
Results: PHP7.2
+ total time foreach: 0.012457847595215
- total time map: 0.098356962203979
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment