note 104642 added to function.is-array

From: Date: Mon, 27 Jun 2011 18:31:36 +0000
Subject: note 104642 added to function.is-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-181007@lists.php.net to get a copy of this message
Wanted to do a little benchmark test to compare the different techniques I saw below. After **10,000,000** runs, seems there is a clear winner (skip to very bottom for results): <?php /* thank magentix at gmail dot com for the bench code--I used it as-is, and it is featured below--but including it made my post too long >.< */ // Test 1 function is_assoc ($arr) { return (is_array($arr) && (!count($arr) || count(array_filter(array_keys($arr),'is_string')) == count($arr))); } // Test 2 function is_assoc_2 ($arr) { return (is_array($arr) && count(array_filter(array_keys($arr),'is_string')) == count($arr)); } // Test 3! function is_assoc_3($arr) { return ((array) $arr !== $arr); } // Test 4 function is_assoc_4($arr) { return ((array) $arr == $arr); } ?> And the results were: Test 1: $a: 10.238099098206 $b: 10.329932928085 $c: 15.229719161987 $d: 53.851320028305 $e: 55.75145816803 $f: 58.900161981583 Test 2: $a: 10.571290969849 $b: 10.291799068451 $c: 32.177886962891 $d: 51.278934001923 $e: 51.932680845261 $f: 52.923846960068 Test 3: $a: 10.674491882324 $b: 10.61211514473 $c: 9.6466329097748 $d: 12.949640035629 $e: 13.294473171234 $f: 13.734415054321 Test 4: $a: 10.781308889389 $b: 10.638602972031 $c: 10.142931938171 $d: 14.474587202072 $e: 14.962183952332 $f: 15.684457063675 Note that the test was preformed on my server stack (instead of the PHP command line). I did this on purpose, due to the fact that the code would be running on *my* stack in production if it were to be used ;) All that really matters, however, are the percentages--which are as follows: Test 1 was the slowest of the four, as I'm sure you see. Test 2: $a and $b: 3.5% slower than Test 1 $c: 111% slower than Test 1 $d: ~5% faster than Test 1 $e: ~8% faster than Test 1 $f: ~11% faster than Test 1 Test 3: $a and $b were ever-so slightly (~1%) slower than Test 2, and even slower (4.26%) than Test 1. $c: This is one of the interesting ones. $c was not only ~37% faster than Test 1, but ~70% faster than Test 2! $d/$e/$f: Now this is where you see the astronomical difference! On average, these three were ~74% faster than Test 2 and ~80% faster than Test 1! Test 4 was slower than Test 3 in every statistic. So, depending on what you're expecting the vast majority of your is_array() function calls to be run against, Test 3's is_assoc() function reigns supreme! ---- Server IP: 69.147.83.197 Probable Submitter: 68.57.233.153 ---- Manual Page -- http://www.php.net/manual/en/function.is-array.php Edit -- https://master.php.net/note/edit/104642 Del: integrated -- https://master.php.net/note/delete/104642/integrated Del: useless -- https://master.php.net/note/delete/104642/useless Del: bad code -- https://master.php.net/note/delete/104642/bad+code Del: spam -- https://master.php.net/note/delete/104642/spam Del: non-english -- https://master.php.net/note/delete/104642/non-english Del: in docs -- https://master.php.net/note/delete/104642/in+docs Del: other reasons-- https://master.php.net/note/delete/104642 Reject -- https://master.php.net/note/reject/104642 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#181007) next »