Created
August 2, 2010 19:29
-
-
Save banks/505177 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Example to accompany blog post: | |
// http://blog.banksdesigns.co.uk/post/when-not-to-use-arrays-in-php | |
ini_set('memory_limit', '512M'); | |
$array_2d = $array_strings = array(); | |
$mem_start = memory_get_usage(); | |
for ($i = 0; $i < 250000; $i++) | |
{ | |
$array_2d[] = array(123,321,234,654); | |
} | |
$mem_2d = memory_get_usage(); | |
print "Memory for 2D:\t\t".round(($mem_2d - $mem_start)/1048576, 2)."MB\n"; | |
for ($i = 0; $i < 250000; $i++) | |
{ | |
$array_strings[] = '123,321,234,654'; | |
} | |
$mem_strings = memory_get_usage(); | |
print "Memory strings:\t\t".round(($mem_strings - $mem_2d)/1048576, 2)."MB\n"; | |
// Speed test - change the third element for each value to 999 | |
$start_time = microtime(TRUE); | |
foreach ($array_2d as $id => $values) | |
{ | |
$array_2d[$id][2] = 999; | |
} | |
$time_2d = microtime(TRUE); | |
print "Time for 2D:\t\t".round($time_2d - $start_time, 2)." seconds\n"; | |
foreach ($array_strings as $id => $values) | |
{ | |
$values = explode(',', $values); | |
$values[2] = 999; | |
$array_strings[$id] = implode(',', $values); | |
} | |
$time_strings = microtime(TRUE); | |
print "Time strings:\t\t".round($time_strings - $time_2d, 2)." seconds\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment