<?php // Disable garbage collection to avoid interference gc_disable(); // Helper function to calculate execution time function benchmark(callable $function) { $start = microtime(true); $function(); $end = microtime(true); return $end - $start; } // Create a deeply nested array function createNestedArray($depth = 3, $width = 10) { $data = []; for ($i = 0; $i < $width; $i++) { $data["level_$i"] = ($depth > 1) ? createNestedArray($depth - 1, $width) : rand(1, 100); } return $data; } // Create a deeply nested object function createNestedObject($depth = 3, $width = 10) { $obj = new stdClass(); for ($i = 0; $i < $width; $i++) { $propName = "level_$i"; $obj->$propName = ($depth > 1) ? createNestedObject($depth - 1, $width) : rand(1, 100); } return $obj; } // Function to modify data in nested array function modifyArray(&$data, $depth = 3) { foreach ($data as &$value) { if ($depth > 1 && is_array($value)) { modifyArray($value, $depth - 1); } else { $value *= 2; // Data alteration } } } // Function to modify data in nested object function modifyObject(&$obj, $depth = 3) { foreach ($obj as &$value) { if ($depth > 1 && is_object($value)) { modifyObject($value, $depth - 1); } else { $value *= 2; // Data alteration } } } // Initialize nested structures $depth = 3; $width = 10; $nestedArray = createNestedArray($depth, $width); $nestedObject = createNestedObject($depth, $width); // Benchmark for array $arrayTime = benchmark(function() use (&$nestedArray, $depth) { modifyArray($nestedArray, $depth); }); // Benchmark for object $objectTime = benchmark(function() use (&$nestedObject, $depth) { modifyObject($nestedObject, $depth); }); echo "Array Time: " . $arrayTime . " seconds\n"; echo "Object Time: " . $objectTime . " seconds\n"; gc_enable(); // Re-enable garbage collection ?>
You have javascript disabled. You will not be able to edit any code.