【发布时间】:2015-05-13 02:05:47
【问题描述】:
我想提前为这篇冗长的帖子道歉,但我想让你清楚地了解我的问题。任何帮助表示赞赏。
我有一个名为 $data 的数组,格式如下:
... ...
给定一个任意的开始日期,我需要在数组中搜索匹配的日期:
//$holidays is an array with dates of public holidays which are not considered business days
$search_results = array();
$minDate = -10;
$maxDate = 100;
$start_date = "2015-02-25";
echo "Before loop: " . xdebug_time_index() . "<br>";
for ($i=$minDate; $i<=$maxDate; $i++) {
if (in_array_r(getBusinessDay(new DateTime($start_date), $holidays, $i), $data)){
$a_date = getBusinessDay(new DateTime($start_date), $holidays, $i);
$a_key = array_search($a_date, array_column($data, "date"));
$search_results[]=array($i, $data[$a_key]["data"]);
}
}
echo "After loop: " . xdebug_time_index() . "<br>";
var_dump($search_results);
但是,这段代码 sn-p 可能会在页面加载时运行 10-15 次,每次执行需要很长时间(在更大的数组上至少需要 6 秒):
能否请您帮助我了解导致此延迟的代码部分以及如何加快此过程?
提前感谢您的帮助。
下面是代码sn-p中用到的函数:
function getBusinessDay($startdate, $holidays, $days) {
$calculator = new BusinessDaysCalculator($startdate, $holidays, [BusinessDaysCalculator::SATURDAY, BusinessDaysCalculator::SUNDAY]);
$calculator->addBusinessDays($days);
$result = $calculator->getDate()->format('Y-m-d');
unset($calculator);
return $result;
}
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
以及返回下一个工作日的计算器(跳过周末和 $holidays 数组中的任何日期):
class BusinessDaysCalculator {
const MONDAY = 1;
const TUESDAY = 2;
const WEDNESDAY = 3;
const THURSDAY = 4;
const FRIDAY = 5;
const SATURDAY = 6;
const SUNDAY = 7;
/**
* @param DateTime $startDate Date to start calculations from
* @param DateTime[] $holidays Array of holidays, holidays are no considered business days.
* @param int[] $nonBusinessDays Array of days of the week which are not business days.
*/
public function __construct(DateTime $startDate, array $holidays, array $nonBusinessDays) {
$this->date = $startDate;
$this->holidays = $holidays;
$this->nonBusinessDays = $nonBusinessDays;
}
public function addBusinessDays($howManyDays) {
$i = 0;
while ($i < abs($howManyDays)) {
if ($howManyDays < 0) {
$this->date->modify("-1 day");
} else {
$this->date->modify("+1 day");
}
if ($this->isBusinessDay($this->date)) {
$i++;
}
}
}
public function getDate() {
return $this->date;
}
private function isBusinessDay(DateTime $date) {
if (in_array((int)$date->format('N'), $this->nonBusinessDays)) {
return false; //Date is a nonBusinessDay.
}
foreach ($this->holidays as $day) {
if ($date->format('Y-m-d') == $day->format('Y-m-d')) {
return false; //Date is a holiday.
}
}
return true; //Date is a business day.
}
}
更新 1: 我将 $data 数组的结构更新为
然后循环到:
for ($i=$minDate; $i <= $maxDate; $i++) {
$day = getBusinessDay(new DateTime($start_date), $holidays, $i);
if (array_key_exists($day, $data)) {
$search_results[]=array($i, $data[$day]);
}
}
时代只进步了一点点:
是array_key_exists导致延迟的原因吗?
更新 2: 这是 $holidays 数组(它是静态的,总是一样的):
【问题讨论】:
-
您的数据中的日期列是否唯一?
-
是的,相同的日期不会在 $data 数组中出现两次。
-
array_filter 可能会更快
-
Profiling PHP Scripts。您可以使用 *nix 上的 KCacheGrind 或 Windows 上的 WinCacheGrind 来读取分析文件。
-
把
$i++改成++$i,这样会减少一点时间。
标签: php arrays performance search