简体   繁体   中英

Why is the array_unique version faster

I was discussing with a colleague which was faster and we did a small benchmark:

function uniqueByKey(){
    $strings = [];
    $stopwatch->start('test');

    for ($i = 1; $i <= 1000000; $i++) {
        $strings[RandomStringUtil::generate()] = 1;
    }

    $outcome = $stopwatch->stop('test');
    die($outcome->getDuration()/1000);

    self::assertSame(25, count($strings));
}

And

function uniqueByFunction(){
    $strings = [];
    $stopwatch->start('test');

    for ($i = 1; $i <= 1000000; $i++) {
        $strings[] = RandomStringUtil::generate();
    }
    $strings = array_unique($strings);

    $outcome = $stopwatch->stop('test');
    die($outcome->getDuration()/1000);

    self::assertSame(25, count($strings));
}

I was expecting method uniqueByKey to be faster, as accessing arrays by key is very fast, and the array unique now had 1.000.000 values to check for uniqueness.

However, uniqueByKey took (on average of a few runs) a consistent ~12.5sec, the uniqueByFunction only about ~11.8sec.

In our use case we only loop 25x, so the difference isnt worth noting, however we are curious why the latter is faster than the first. Can anyone explain?

If it matters, tested on a MacbookPro 2020 edition)

I try this:

$base = [];
for ($i = 0; $i < 1000000; $i++) {
    $base[] = md5($i);
}

$start = microtime(true);
$array1 = [];
foreach ($base as $random) {
    $array1[$random] = 1;
}
$end = microtime(true);
echo $end - $start . PHP_EOL;

$start = microtime(true);
$array2 = [];
foreach ($base as $random) {
    $array2[] = $random;
}
$array2 = array_unique($array2);
$end = microtime(true);
echo $end - $start . PHP_EOL;

and I got the same result as you, but if I try this:

$base = [];
for ($i = 0; $i < 1000000; $i++) {
    $base[] = md5($i);
    $base[] = md5($i);
    $base[] = md5($i);
}

$start = microtime(true);
$array1 = [];
foreach ($base as $random) {
    $array1[$random] = 1;
}
$end = microtime(true);
echo $end - $start . PHP_EOL;

$start = microtime(true);
$array2 = [];
foreach ($base as $random) {
    $array2[] = $random;
}
$array2 = array_unique($array2);
$end = microtime(true);
echo $end - $start . PHP_EOL;

I got the opposite result. array_unique is not always faster

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM