简介
laravel框架提供了一些实用性很强的类,诸如Collection,Carbon等。同样是遍历元素,那到底Collection与foreach哪家强?
检验代码
<?php
require("TestCase.php");
class SpeedTest extends TestCase
{
public function testCollection()
{
collect(range(1, 1500000))->map(function ($value) {
return $value * 2;
})->filter(function ($value) {
return $value > 10000;
});
}
public function testForeach()
{
$double_me = [];
foreach (range(1, 1500000) as $value) {
$double_me[] = $value * 2;
}
$filter_me = [];
foreach ($double_me as $key => $value) {
if ($value > 10000) {
$filter_me[$key] = $value;
}
}
}
}
检验结果
参考链接
http://qiita.com/acro5piano/items/96b6fe8e4b465447d59c