Add
{code} {code} == == * * _ _ [] * #

php array assoc detection

2017-10-16 14:54:19
<?php

if(!isset($argv[1])){
    echo "Usage: ".$argv[0]." (number of iterations)
";
    exit(1);
}

/**
 * Arrays to check
 */
$tests = array(
    array(array(
        'foo' => 'bar',
    )),
    array(array(
        'bar',
        'foo' => 'bar',
        'baz',
    )),
    array(null),
    array(true),
    array(false),
    array(0),
    array(1),
    array(0.0),
    array(1.0),
    array('string'),
    array(array(0, 1, 2)),
    array(new stdClass),
    array_fill(0,1000,uniqid()),                // big numeric array

    array_fill_keys(range(2,1000,3),uniqid()),  // big misaligned numeric array (=associative)

    array_fill_keys(                            // big associative array
        str_split(
            str_repeat(uniqid('',true),100),
            3
        ),
        true
    )
);

$iterations = (int)$argv[1];

/**
 * Common methods to check associative array
 */
$methods = array(
    'method1 (array_values check)' => function($array){
        return array_values($array) !== $array;
    },
    'method2 (array_keys comparison)' => function($array){
        $array = array_keys($array); return ($array !== array_keys($array));
    },
    'method3 (array_filter of keys)' => function($array){
        return count(array_filter(array_keys($array), 'is_string')) > 0;
    },
    'array_keys !== range' => function($array) {
        return array_keys($array) !== range(0, count($array) - 1);
    },
);

$keys = array_keys($methods);
shuffle($keys);
$methods_new = [];
foreach ($keys as $key) {
    $methods_new[$key] = $methods[$key];
}
$methods = $methods_new;
unset($methods_new);


if (!extension_loaded('pcntl') && !dl('pcntl.so')) die("no pcntl.so
");

foreach($methods as $name=>$func){
    switch (pcntl_fork()) {
    case -1: die("couldnot fork
");
    case 0:
    echo "Testing $name - $iterations iterations
";
    $time = microtime(true);

    for($x=0;$x<$iterations;$x++){
        foreach($tests as $array){
            $func($array);
        }
    }

    /**
     * Show results
     */
    $totalTime = (microtime(true) - $time);
    $avgTime = $totalTime / ($iterations * count($tests));

    echo "  Total time: ".number_format($totalTime,5,'.',' ')." s
";
    echo "  Average   : ".number_format($avgTime*1e6,5,'.',' ')." us / test 
";
    echo "  Mempeak   : ".number_format(memory_get_peak_usage(),0,'.',' ')." bytes 
";
    echo "
";
    exit(0);
    break;
    default: pcntl_wait($status);
    }
}