RobertP Posted December 12, 2012 Share Posted December 12, 2012 results: 0.839771 << isset empty 2.519401 << array_key_exists empty 2.476911 << in_array empty 0.812198 << isset full 2.472904 << array_key_exists full Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/test.php on line 58 code: <?php $iterations = 1000000; $data = array(); $dataFull = array(); for ($i = 0; $i <= 1024; $i++) { $dataFull[$i] = str_repeat('1', 1024); } $startTime = microtime(true); for ($i = 0; $i <= $iterations; $i++) { $foo = isset($data[$i]); } $totalTime = microtime(true) - $startTime; echo number_format($totalTime, 6) . ' << isset empty<br />'; //==== $startTime = microtime(true); for ($i = 0; $i <= $iterations; $i++) { $foo = array_key_exists($i, $data); } $totalTime = microtime(true) - $startTime; echo number_format($totalTime, 6) . ' << array_key_exists empty<br />'; //==== $startTime = microtime(true); for ($i = 0; $i <= $iterations; $i++) { $foo = in_array($i, $data); } $totalTime = microtime(true) - $startTime; echo number_format($totalTime, 6) . ' << in_array empty<br />'; echo '<br />'; $startTime = microtime(true); for ($i = 0; $i <= $iterations; $i++) { $foo = isset($dataFull[$i]); } $totalTime = microtime(true) - $startTime; echo number_format($totalTime, 6) . ' << isset full<br />'; //==== $startTime = microtime(true); for ($i = 0; $i <= $iterations; $i++) { $foo = array_key_exists($i, $dataFull); } $totalTime = microtime(true) - $startTime; echo number_format($totalTime, 6) . ' << array_key_exists full<br />'; //==== $startTime = microtime(true); for ($i = 0; $i <= $iterations; $i++) { $foo = in_array($i, $dataFull); } $totalTime = microtime(true) - $startTime; echo number_format($totalTime, 6) . ' << in_array full<br />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/271889-array-benchmarking/ Share on other sites More sharing options...
kicken Posted December 12, 2012 Share Posted December 12, 2012 Did you have a point/question in this post? Or just posting an FYI thing? FYI, 1) isset is a language construct, array_key_exists is a function call. Function calls have extra overhead, thus are slower 2) You're not using in_array properly. in_array tests if a particular value exists in an array, not if a particular key exists 3) You should use isset(), except under one condition: If you need to know if the key exists, but NULL is an acceptable value for that key (eg if it can contain say a DateTime object or null for none). Quote Link to comment https://forums.phpfreaks.com/topic/271889-array-benchmarking/#findComment-1398857 Share on other sites More sharing options...
trq Posted December 12, 2012 Share Posted December 12, 2012 Your point? Quote Link to comment https://forums.phpfreaks.com/topic/271889-array-benchmarking/#findComment-1398870 Share on other sites More sharing options...
RobertP Posted December 12, 2012 Author Share Posted December 12, 2012 this post was more of a FYI, but my reason for testing was to count how many unique visitors i had, so i would add each unique ip to the array (does not matter if in the key, or the value of the array entry). Quote Link to comment https://forums.phpfreaks.com/topic/271889-array-benchmarking/#findComment-1398913 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.