Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@predakanga
Last active July 24, 2018 12:36
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 predakanga/22e5b427c948071e8d292ed9b5c95c56 to your computer and use it in GitHub Desktop.
Save predakanga/22e5b427c948071e8d292ed9b5c95c56 to your computer and use it in GitHub Desktop.
<?php
function indirectStringToUpper($word) {
return strtoupper($word);
}
class FuncBench {
public function benchLoopDirectFunction() {
$words = ['i', 'am', 'not', 'shouting'];
$shouts = [];
foreach($words as $word) {
$shouts[] = strtoupper($word);
}
}
public function benchLoopIndirectFunction() {
$words = ['i', 'am', 'not', 'shouting'];
$shouts = [];
foreach($words as $word) {
$shouts[] = indirectStringToUpper($word);
}
}
public function benchLoopClosure() {
$words = ['i', 'am', 'not', 'shouting'];
$shouts = [];
$closure = function($word) {
return strtoupper($word);
};
foreach($words as $word) {
$shouts[] = $closure($word);
}
}
public function benchMapDirectFunction() {
$words = ['i', 'am', 'not', 'shouting'];
$shouts = [];
$shouts = array_map('strtoupper', $words);
}
public function benchMapIndirectFunction() {
$words = ['i', 'am', 'not', 'shouting'];
$shouts = [];
$shouts = array_map('indirectStringToUpper', $words);
}
public function benchMapClosure() {
$words = ['i', 'am', 'not', 'shouting'];
$shouts = [];
$shouts = array_map(function($word) {
return strtoupper($word);
}, $words);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment