note 82046 modified in function.array-count-values by felipe

From: Date: Tue, 25 Mar 2008 21:51:41 +0000
Subject: note 82046 modified in function.array-count-values by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137578@lists.php.net to get a copy of this message
byron at byronrode dot co dot za, here are some benchmarks. <?php $haystack = Array(); for ($i = 0; $i < 1000000; $i++) { $haystack[] = rand(1, 2000); } $needle = rand(1, 2000); echo "__array_count_values()__\n"; $start = microtime(true); $startmem = memory_get_usage(); $counts = array_count_values($haystack); $mem = memory_get_usage()-$startmem; echo 'Count:'.$counts[$needle]."\n"; echo 'Time:'.(microtime(true) - $start)."\n"; echo 'Memory:'.$mem."\n\n"; echo "__array_keys()__\n"; $start = microtime(true); $startmem = memory_get_usage(); $keys = array_keys($haystack, $needle); $mem = memory_get_usage()-$startmem; echo 'Count:'.count($keys)."\n"; echo 'Time:'.(microtime(true) - $start)."\n"; echo 'Memory:'.$mem."\n\n"; echo '__$needle_array[]__'."\n"; $start = microtime(true); $startmem = memory_get_usage(); $x = count($haystack); for($i = 0; $i < $x; $i++){ if($haystack[$i] == $needle){ $needle_array[] = $haystack[$i]; } } $mem = memory_get_usage()-$startmem; $number_of_instances = count($needle_array); echo 'Count:'.$number_of_instances."\n"; echo 'Time:'.(microtime(true) - $start)."\n"; echo 'Memory:'.$mem."\n\n"; echo '__$number_of_instances++__'."\n"; $start = microtime(true); $startmem = memory_get_usage(); $x = count($haystack); $number_of_instances = 0; for($i = 0; $i < $x; $i++){ if($haystack[$i] == $needle){ $number_of_instances++; } } $mem = memory_get_usage()-$startmem; echo 'Count:'.$number_of_instances."\n"; echo 'Time:'.(microtime(true) - $start)."\n"; echo 'Memory:'.$mem."\n\n"; ?> [www]mytemp$ php array_count_test.php __array_count_values()__ Count:515 Time:0.0607650279999 Memory:120328 __array_keys()__ Count:515 Time:0.0869138240814 Memory:33016 __$needle_array[]__ Count:515 Time:0.259949922562 Memory:24792 __$number_of_instances++__ Count:515 Time:0.258481025696 Memory:0 However, when you use an array of strings by calling md5(rand(1, 2000)), the performance boosts become less significant: __array_count_values()__ Count:499 Time:0.491794109344 Memory:184328 __array_keys()__ Count:499 Time:0.36399102211 Memory:30072 __$needle_array[]__ Count:499 Time:0.568728923798 Memory:22104 __$number_of_instances++__ Count:499 Time:0.574353933334 Memory:0 Results are similar for string->string haystacks with foreach traversal. --was-- [Mods, please delete my previous note.] byron at byronrode dot co dot za, here are some benchmarks. <?php $haystack = Array(); for ($i = 0; $i < 1000000; $i++) { $haystack[] = rand(1, 2000); } $needle = rand(1, 2000); echo "__array_count_values()__\n"; $start = microtime(true); $startmem = memory_get_usage(); $counts = array_count_values($haystack); $mem = memory_get_usage()-$startmem; echo 'Count:'.$counts[$needle]."\n"; echo 'Time:'.(microtime(true) - $start)."\n"; echo 'Memory:'.$mem."\n\n"; echo "__array_keys()__\n"; $start = microtime(true); $startmem = memory_get_usage(); $keys = array_keys($haystack, $needle); $mem = memory_get_usage()-$startmem; echo 'Count:'.count($keys)."\n"; echo 'Time:'.(microtime(true) - $start)."\n"; echo 'Memory:'.$mem."\n\n"; echo '__$needle_array[]__'."\n"; $start = microtime(true); $startmem = memory_get_usage(); $x = count($haystack); for($i = 0; $i < $x; $i++){ if($haystack[$i] == $needle){ $needle_array[] = $haystack[$i]; } } $mem = memory_get_usage()-$startmem; $number_of_instances = count($needle_array); echo 'Count:'.$number_of_instances."\n"; echo 'Time:'.(microtime(true) - $start)."\n"; echo 'Memory:'.$mem."\n\n"; echo '__$number_of_instances++__'."\n"; $start = microtime(true); $startmem = memory_get_usage(); $x = count($haystack); $number_of_instances = 0; for($i = 0; $i < $x; $i++){ if($haystack[$i] == $needle){ $number_of_instances++; } } $mem = memory_get_usage()-$startmem; echo 'Count:'.$number_of_instances."\n"; echo 'Time:'.(microtime(true) - $start)."\n"; echo 'Memory:'.$mem."\n\n"; ?> [www]mytemp$ php array_count_test.php __array_count_values()__ Count:515 Time:0.0607650279999 Memory:120328 __array_keys()__ Count:515 Time:0.0869138240814 Memory:33016 __$needle_array[]__ Count:515 Time:0.259949922562 Memory:24792 __$number_of_instances++__ Count:515 Time:0.258481025696 Memory:0 However, when you use an array of strings by calling md5(rand(1, 2000)), the performance boosts become less significant: __array_count_values()__ Count:499 Time:0.491794109344 Memory:184328 __array_keys()__ Count:499 Time:0.36399102211 Memory:30072 __$needle_array[]__ Count:499 Time:0.568728923798 Memory:22104 __$number_of_instances++__ Count:499 Time:0.574353933334 Memory:0 Results are similar for string->string haystacks with foreach traversal. http://php.net/manual/en/function.array-count-values.php

« previous php.notes (#137578) next »