$map = \sclable\arrayFunctions\ArrayWrap::create([]); $wrapMap = \sclable\arrayFunctions\ArrayWrap::create([]); for ($i = 0; $i < 100; $i++) { // foreach $fTextList = $textList; $start = microtime(true); foreach ($fTextList as &$text) { $text = htmlentities($text); } $end = microtime(true); $foreach->push($end - $start); // map $wTextList = $textList; $start = microtime(true); $wTextList = array_map('htmlentities', $wTextList); $end = microtime(true); $map->push($end - $start); // ArrayWrap map $mTextList = $textList; $start = microtime(true); $mTextList = \sclable\arrayFunctions\ArrayWrap::create($mTextList)->apply('htmlentities'); $end = microtime(true); $wrapMap->push($end - $start); } echo 'foreach: ' . printResult($foreach) . PHP_EOL; echo 'map: ' . printResult($map) . PHP_EOL; echo 'wrapMap: ' . printResult($wrapMap) . PHP_EOL; function printResult(\sclable\arrayFunctions\ArrayWrap $results) { return "min: {$results->min()} / max: {$results->max()} / avg: {$results->avg()} || sum: {$results->sum()}"; }
$foreach->push($end - $start); // map $start = microtime(true); $assigned2 = array_map(function ($instance) { return $instance->id; }, $instances); $end = microtime(true); $mapMethod->push($end - $start); // method call $start = microtime(true); $assigned2 = array_map('extractId', $instances); $end = microtime(true); $map->push($end - $start); // ArrayWrap map $start = microtime(true); $assigned3 = \sclable\arrayFunctions\ArrayWrap::create($instances)->map(function ($instance) { return $instance->id; }); $end = microtime(true); $wrapMap->push($end - $start); } echo 'foreach: ' . printResult($foreach) . PHP_EOL; echo 'map: ' . printResult($map) . PHP_EOL; echo 'map method: ' . printResult($mapMethod) . PHP_EOL; echo 'wrapMap: ' . printResult($wrapMap) . PHP_EOL; function printResult(\sclable\arrayFunctions\ArrayWrap $results) { return "min: {$results->min()} / max: {$results->max()} / avg: {$results->avg()} || sum: {$results->sum()}"; } function extractId($instance) {
<?php /** * ---------------------------------------------------------------------------- * This code is part of the Sclable Business Application Development Platform * and is subject to the provisions of your License Agreement with * Sclable Business Solutions GmbH. * * @copyright (c) 2016 Sclable Business Solutions GmbH * ---------------------------------------------------------------------------- */ return \sclable\arrayFunctions\ArrayWrap::create(range(1, 10000))->map(function ($id) { return (object) ['id' => $id, 'name' => 'name - ' . $id]; })->getRaw();
<?php /** * ---------------------------------------------------------------------------- * This code is part of the Sclable Business Application Development Platform * and is subject to the provisions of your License Agreement with * Sclable Business Solutions GmbH. * * @copyright (c) 2016 Sclable Business Solutions GmbH * ---------------------------------------------------------------------------- */ use sclable\arrayFunctions\ArrayWrap; require_once dirname(__DIR__) . '/vendor/autoload.php'; $numbers = ArrayWrap::range(-10000, -2)->shuffle()->getRaw(); // old $numbers = array_pad([], 10, 0); foreach ($numbers as &$number) { $number = rand(); } $max = null; foreach ($numbers as $number) { $max = $max === null ? $number : max($max, $number); } echo $max . PHP_EOL; // fp echo ArrayWrap::create([])->pad(10, 0)->map(function () { return rand(); })->reduce('max');
for ($i = 0; $i < 100; $i++) { // foreach $start = microtime(true); $max = null; foreach ($numbers as $number) { if ($max === null) { $max = $number; continue; } $max = max($max, $number); } $end = microtime(true); $foreach->push($end - $start); // map $start = microtime(true); $max = array_reduce($numbers, 'max', null); $end = microtime(true); $map->push($end - $start); // ArrayWrap map $start = microtime(true); $max = ArrayWrap::create($numbers)->max(); $end = microtime(true); $wrapSum->push($end - $start); } echo 'foreach: ' . printResult($foreach) . PHP_EOL; echo 'map: ' . printResult($map) . PHP_EOL; echo 'wrapMap: ' . printResult($wrapSum) . PHP_EOL; function printResult(ArrayWrap $results) { return "min: {$results->min()} / max: {$results->max()} / avg: {$results->avg()} || sum: {$results->sum()}"; }
public function testReverseWithKeyPreservation() { $this->assertEquals([2 => 2, 1 => 1, 0 => 0], ArrayWrap::create([0 => 0, 1 => 1, 2 => 2])->reverse(true)->getRaw()); }