Skip to content

Instantly share code, notes, and snippets.

@zaru
Created February 12, 2014 01:50
Show Gist options
  • Select an option

  • Save zaru/8948475 to your computer and use it in GitHub Desktop.

Select an option

Save zaru/8948475 to your computer and use it in GitHub Desktop.
in_array vs array_unique
<?php
$start = microtime(true);
$lists = array();
for ($i=0; $i<1000000; $i++) {
$key = rand(1,500);
if (!in_array($key, $lists)) {
$lists[] = $key;
}
}
$end = microtime(true);
echo $end - $start . "\n";
$start = microtime(true);
$lists = array();
for ($i=0; $i<1000000; $i++) {
$lists[] = rand(1,500);
}
$newLists = array_unique($lists);
$end = microtime(true);
echo $end - $start . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment