Thanks for that ... I may want to bookmark 3v4l.org 😉
My calcs in a testbed also seem to indicate array_unique over 100 in_arrays, or 10K in_arrays, but it appears to be a very low-impact difference at the low scale of 100 ...
1st implementation, 100 iterations
.001183
.00114
.001232999---
.0012310000000001
.00112
2nd implementation, 100 iterations
.002235
.00213
.002642
.002047
.002003
1st implementation, 10K iterations
.132486
.129009
.131604
.128563
.12823
2nd implementation, 10K iterations:
.141611
.142324
.151341
.142888
.142646
1st implementation code:
<?php
$start = microtime();
$users = array(41,73,83,94,922,1024,1554);
$userct = count( $users );
$n = 0;
$out = array();
while ( $n < 100 ) {
$x = rand(0,($userct-1));
$this_user = $users[$x];
$seen_users[$n] = $this_user;
$n++;
}
$seen_users = array_unique($seen_users);
$end = microtime();
echo "Time: ", ($end - $start).PHP_EOL;
print_r($seen_users);
2nd implementation code:
<?php
$start = microtime();
$users = array(41,73,83,94,922,1024,1554);
$userct = count( $users );
$n = 0;
$out = array();
while ( $n < 100 ) {
$x = rand(0,($userct-1));
$this_user = $users[$x];
if ( ! in_array($this_user, $seen_users) ) {
$seen_users[$n] = $this_user;
}
$n++;
}
$end = microtime();
echo "Time: ", ($end - $start).PHP_EOL;
print_r($seen_users);
Perhaps most surprising is the 10K test, which doesn't show array_unique being nearly twice as fast as 10K in_array()s ... scratching my head on that one.