<?php namespace BRS\PerformanceDiff; $executor = new Executor('Testing finding a Key by value using foreach and array_search.', 100, Executor::TARE | Executor::PROGRESS); $executor->setPrepCallback(function ($executor) { $executor->setPayload(range(10000, 1000000)); }); $executor->setTareFunction(function ($payload) { $find = rand(0, 1000000); return null; }); $executor->setRerun(1); $executor->addTest(new Test('Search', function ($payload) { $find = rand(0, 1000000); return array_search($find, $payload); })); $executor->addTest(new Test('FlipIsSet', function ($payload) { $find = rand(0, 1000000); $payload = array_flip($payload); return isset($payload[$find]) ? $payload[$find] : null; })); $executor->addTest(new Test('ForEach', function ($payload) { $find = rand(0, 1000000); foreach ($payload as $key => $value) { if ($value == $find) { return $key; } } return ''; }));
<?php namespace BRS\PerformanceDiff; $executor = new Executor('Testing getting a char in a string using indexing vs substrings.', 1000000, Executor::TARE | Executor::PROGRESS); $executor->setPrepCallback(function ($executor) { $string = ''; $pool = '1234567890qwertyuiopasdfghjklzxcvbnm'; while (strlen($string) > 10000) { // hint hint... $string .= $string[rand(0, strlen($pool) - 1)]; } $executor->setPayload(array(strlen($string), $string)); }); $executor->setTareFunction(function ($payload) { return rand(0, $payload[0] - 1); }); $executor->setRerun(10); $executor->addTest(new Test('Index', function ($payload) { return $payload[1][rand(0, $payload[0] - 1)]; })); $executor->addTest(new Test('Substr', function ($payload) { return substr($string, rand(0, $payload[0]), 1); })); $executor->execute(); $executor->log(basename(__FILE__) . '.' . time());
<?php namespace BRS\PerformanceDiff; $executor = new Executor('Testing iteration on range() vs for loop.', 100000000, Executor::TARE | Executor::PROGRESS); $executor->setRerun(5); $executor->setPayload(4800000); $executor->addTest(new Test('Range', function ($payload) { foreach (range(1, $payload) as $i) { } return NULL; })); $executor->addTest(new Test('For Loop', function ($payload) { for ($i = 1; $i <= $payload; $i++) { } return NULL; })); $executor->execute(); $executor->log(basename(__FILE__) . '.' . time());