note 98453 modified in function.array-unique by danbrown

From: Date: Wed, 16 Jun 2010 14:19:06 +0000
Subject: note 98453 modified in function.array-unique by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170180@lists.php.net to get a copy of this message
It's often faster to use a foreache and array_keys than array_unique: <?php $max = 1000000; $arr = range(1,$max,3); $arr2 = range(1,$max,2); $arr = array_merge($arr,$arr2); $time = -microtime(true); $res1 = array_unique($arr); $time += microtime(true); echo "deduped to ".count($res1)." in ".$time; // deduped to 666667 in 32.300781965256 $time = -microtime(true); $res2 = array(); foreach($arr as $key=>$val) { $res2[$val] = true; } $res2 = array_keys($res2); $time += microtime(true); echo "<br />deduped to ".count($res2)." in ".$time; // deduped to 666667 in 0.84372591972351 ?> --was-- It's often faster to use a foreache and array_keys than array_unique: <? $max = 1000000; $arr = range(1,$max,3); $arr2 = range(1,$max,2); $arr = array_merge($arr,$arr2); $time = -microtime(true); $res1 = array_unique($arr); $time += microtime(true); echo "deduped to ".count($res1)." in ".$time; // deduped to 666667 in 32.300781965256 $time = -microtime(true); $res2 = array(); foreach($arr as $key=>$val) { $res2[$val] = true; } $res2 = array_keys($res2); $time += microtime(true); echo "<br />deduped to ".count($res2)." in ".$time; // deduped to 666667 in 0.84372591972351 ?> http://php.net/manual/en/function.array-unique.php

« previous php.notes (#170180) next »