Important security update, please update to Osclass 3.7.4
At Osclass we have changed our Privacy Policy and Terms of Use in order to adapt them to the new General Data Protection Regulation (GDPR). We want you to know what user data we store, what we need them for, and who we share them with in each specific case. Furthermore, we are making it even easier for you to exercise your right to manage your own data.
Our goal is that you enjoy the best possible experience with our website. As the GDPR comes into force, legislation requires us that you grant us permission—both to us and our partners—to store cookies in your browser. Remember you can find more information about what we do with your data by clicking here.
I accept Osclass SL’s Terms of Use and Cookies Policy and grant them permission to manage my data.
require_once 'Benchmark/Iterate.php';define(MAX_RUN, 500);$data = array(1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);doBenchmark('v1', $data);doBenchmark('v2', $data);doBenchmark('v3', $data);doBenchmark('v4', $data);doBenchmark('v5', $data);doBenchmark('v6', $data);doBenchmark('v7', $data);doBenchmark('v8', $data);function doBenchmark($functionName = null, $arr = null){reset($arr);$benchmark = new Benchmark_Iterate;$benchmark->run(MAX_RUN, $functionName, $arr);$result = $benchmark->get();echo '<br>';printf("%s ran %d times where average exec time %.5f ms",$functionName,$result['iterations'],$result['mean'] * 1000);}function v1($myArray = null) {// Do bad loopfor ($i =0; $i < sizeof($myArray); $i++){echo '<!--' . $myArray[$i] . ' --> ';}}function v2($myArray = null) {// Do better loop// Get the size of array$max = sizeof($myArray);for ($i =0; $i < $max ; $i++){echo '<!--' . $myArray[$i] . ' --> ';}}function v3($myArray = null) {// Do much better loop// Get the size of array$max = sizeof($myArray);for ($i =0; $i < $max ; $i++){// Store the output in a string$output .= '<!--' . $myArray[$i] . ' --> ';}// Echo the output stringecho $output;}function v4($myArray = null) {// Do much better loop// Get the size of array$max = sizeof($myArray);$output = array();for ($i =0; $i < $max ; $i++){// Store the output in a stringarray_push($output, '<!--' .$myArray[$i] .' --> ');}// Echo the output stringecho implode('', $output);}function v5($myArray = null) {// Do much better loop// Get the size of array$max = count($myArray);for ($i =0; $i < $max ; $i++){// Store the output in a stringecho '<!--' . $myArray[$i] . ' --> ';}// Echo the output string}function v6($myArray = null) {// Do much better loop// Get the size of arrayfor ($i =0; $i < count($myArray) ; $i++){// Store the output in a stringecho '<!--' . $myArray[$i] . ' --> ';}// Echo the output string}function v7($myArray = null) {// Do much better loop// Get the size of arrayforeach($myArray as $number){// Store the output in a stringecho '<!--' . $number . ' --> ';}// Echo the output string}function v8($myArray = null) {// Do much better loop// Get the size of array $key = array_keys($myArray); $size = sizeOf($key);for ($i=0; $i<$size; $i++){// Store the output in a stringecho '<!--' . $myArray[$i] . ' --> ';}// Echo the output string}
v1 ran 500 times where average exec time 0.02600 msv2 ran 500 times where average exec time 0.01500 msv3 ran 500 times where average exec time 0.02300 msv4 ran 500 times where average exec time 0.02800 msv5 ran 500 times where average exec time 0.01700 msv6 ran 500 times where average exec time 0.02100 msv7 ran 500 times where average exec time 0.02200 msv8 ran 500 times where average exec time 0.01900 ms