1
哪两个会更快,如何能做到这样的测量这两个会更快
foreach ($posts as $post)
{
$totalikes = count($post["Like"]);
$totacomments = count($post["Comment"]);
$max = ($totalikes < $totacomments)? $totacomments : $totalikes;
for($i=0;$i<$max;$i++)
{
if(isset($post["Like"][$i]))
$users[] = $post["Like"][$i]["user_id"];
if(isset($post["Comment"][$i]))
$users[] = $post["Comment"][$i]["user_id"];
}
}
或
foreach ($posts as $post)
{
foreach ($post["Like"] as $like)
{
$users[] = $like["user_id"];
}
foreach ($post["Comment"] as $comment)
{
$users[] = $comment["user_id"];
}
}
其中两个是更好
为了清晰起见,我会选择第二个。性能差异可能可以忽略不计... – 2010-12-22 09:57:05