Voting

: min(three, nine)?
(Example: nine)

The Note You're Voting On

lrgbx0 at gmail dot com
6 years ago
is_assoc() Benchmark:

<?php

function is_assoc1($array) {
if (!
is_array($array)) return false;
$i = count($array);
while (
$i > 0) unset($array[--$i]);
return (bool)
$array;
}

function
is_assoc2(&$array) {
if (!
is_array($array)) return false;
$i = count($array);
while (
$i > 0) {
if (!isset(
$array[--$i])) return true;
}
return
false;
}

function
is_assoc3(&$array) {
if (!
is_array($array)) return false;
$i = count($array);
while (
$i > 0) {
if (!
array_key_exists(--$i, $array)) return true;
}
return
false;
}

function
is_assoc4($array) {
if (!
is_array($array)) return false;
ksort($array);
foreach (
array_keys($array) as $k => $v) {
if (
$k !== $v) return true;
}
return
false;
}

function
is_assoc5(&$array) {
return
is_array($array) && array_diff_key($array, array_keys($array));
}

$arr1 = array(); // not associative
$arr2 = $arr3 = array('foo', 'bar', 'baz', 'foo', 'bar', 'baz', 'foo', 'bar', 'baz', 'foo'); // not associative
asort($arr3); // not associative, shuffled keys
$arr4 = array('foo', 'bar', 'baz', 'foo', 'bar', null, 'foo', 'bar', 'baz', 'foo'); // not associative but is_assoc2() thinks it is
$arr5 = array(0 => 'foo', 1 => 'bar', 2 => 'baz', 3 => 'foo', 4 => 'bar', 5 => 'baz', 'foo3' => 'foo', 'bar3' => 'bar', 'baz3' => 'baz', 'foo4' => 'foo'); // associative

$i = $j = 0;
$time = array(0.0, 0.0, 0.0, 0.0, 0.0);

for (
$j = 0; $j < 2000; $j++) {
$time[0] -= microtime(true);
for (
$i = 0; $i < 1000; $i++) {
if (
is_assoc1($arr1) || is_assoc1($arr2) || is_assoc1($arr3) || is_assoc1($arr4) || !is_assoc1($arr5)) {
echo
'error';
break;
}
}
$time[0] += microtime(true);
$time[1] -= microtime(true);
for (
$i = 0; $i < 1000; $i++) {
if (
is_assoc2($arr1) || is_assoc2($arr2) || is_assoc2($arr3) || !is_assoc2($arr4) || !is_assoc2($arr5)) { // $arr4 tweaked
echo 'error';
break;
}
}
$time[1] += microtime(true);
$time[2] -= microtime(true);
for (
$i = 0; $i < 1000; $i++) {
if (
is_assoc3($arr1) || is_assoc3($arr2) || is_assoc3($arr3) || is_assoc3($arr4) || !is_assoc3($arr5)) {
echo
'error';
break;
}
}
$time[2] += microtime(true);
$time[3] -= microtime(true);
for (
$i = 0; $i < 1000; $i++) {
if (
is_assoc4($arr1) || is_assoc4($arr2) || is_assoc4($arr3) || is_assoc4($arr4) || !is_assoc4($arr5)) {
echo
'error';
break;
}
}
$time[3] += microtime(true);
$time[4] -= microtime(true);
for (
$i = 0; $i < 1000; $i++) {
if (
is_assoc5($arr1) || is_assoc5($arr2) || is_assoc5($arr3) || is_assoc5($arr4) || !is_assoc5($arr5)) {
echo
'error';
break;
}
}
$time[4] += microtime(true);
}

echo
'is_assoc1(): ' . $time[0] . "\n";
echo
'is_assoc2(): ' . $time[1] . "\n";
echo
'is_assoc3(): ' . $time[2] . "\n";
echo
'is_assoc4(): ' . $time[3] . "\n";
echo
'is_assoc5(): ' . $time[4] . "\n";

?>

is_assoc1() - uses unset(), a bit slow, but mem friendly and no function calls
is_assoc2() - uses isset(), fastest one, but returns TRUE whenever array contains NULL
is_assoc3() - fixed is_assoc2(), uses array_key_exists(), fast and memory friendly, and much smarter than the following (no need to check all those keys)
is_assoc4() - alex' version with proper check and key sorting
is_assoc5() - fixed a bit JTS' version, really nice one, but uses too many functions and checks all keys

Results:

is_assoc1(): 2.1628699302673
is_assoc2(): 1.1079933643341
is_assoc3(): 1.7120850086212
is_assoc4(): 3.9194552898407
is_assoc5(): 1.9509885311127

<< Back to user notes page

To Top