Array vs Obj iteration - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of Array vs Obj iteration.php
- <?php
- $hashes = array();
- for ($x=0;$x<100000;$x++) {
- $hashes[] = sha1(rand(1,1000));
- }
- function iterate($hashes) {
- $count = 0;
- $time_start = microtime(true);
- foreach ($hashes as $hash) {
- $count++;
- if ($count % 1000 == 0) {
- $time_end = microtime(true);
- $time = $time_end - $time_start;
- echo $count.' : '.$time.' : '."\n";
- }
- }
- }
- $ohashes = (object)$hashes;
- iterate($ohashes);
- echo 'Done with object'."\n";
- iterate($hashes);
- echo 'Done with array'."\n";
- unset($hashes);
- unset($ohashes);
- $hashes = array();
- for ($x=0;$x<100000;$x++) {
- $kv = sha1($x);
- $hashes[$kv] = $kv;
- }
- function iterate2($hashes) {
- $count = 0;
- $time_start = microtime(true);
- foreach ($hashes as $key=>$value) {
- $count++;
- if ($count % 1000 == 0) {
- $time_end = microtime(true);
- $time = $time_end - $time_start;
- echo $count.' : '.$time.' : '."\n";
- }
- }
- }
- $ohashes = (object)$hashes;
- iterate2($ohashes);
- echo 'Done with object'."\n";
- iterate2($hashes);
- echo 'Done with array'."\n";