PHP 搜索数组速度

标签 php arrays performance search

我想提前为这篇冗长的帖子道歉,但我想让你清楚地了解我的问题。感谢您的帮助。

我有一个名为 $data 的数组,格式如下:

enter image description here

... ...

给定一个任意的开始日期,我需要在数组中搜索匹配的日期:

//$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);

但是,此代码片段可能会在页面加载时运行 10-15 次,每次执行都需要很长时间(在较大的数组上至少需要 6 秒):

enter image description here

能否请您帮助我了解是哪部分代码导致了这种延迟,我怎样才能加快这个过程?

预先感谢您的帮助。

以下是代码片段中使用的函数:

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 数组的结构更新为

enter image description here

循环到:

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]);
        }
}

时间略有改善:

enter image description here

array_key_exists 是延迟的原因吗?

更新 2: 这是 $holidays 数组(它是静态的,始终相同):

enter image description here

最佳答案

根据您的评论,日期列包含唯一值。 由于您仅在日期列上进行过滤,因此通过日期列为数组编制索引效率更高,因此您应该像这样重组数据:

$data = array(
    '2015-02-19' => 1.35625,
    '2015-02-20' => 1.4015,
    '2015-02-23' => 0.9095,
    '2015-02-24' => 1.0635,
    '2015-02-25' => 1.08775,
    '2015-02-26' => 0.947,
    /* ... */
)

in_array需要在整个数组中循环一个对应于一个日期的元素,这对于大型数组来说可能很慢。 使用此结构,您可以通过使用 $data[$date] 访问数据来立即获取数据。

关于PHP 搜索数组速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28983988/

相关文章:

php - 访问不同服务器上的数据库,无需使用主主/主从复制

用于数字和字母数字字符串的 JavaScript 数组排序函数

arrays - Swift NSCoding - 如何读取编码数组

java - 服务单元测试

c# - `unsafe`在Java和C#中的表现

mysql - SQL查询不使用索引

php - 仅允许用户使用数据库中的某些信息

php - 如何减少获得此结果所需的查询数量

php - 将mysql日期时间转换为简单文本

arrays - 如何在T-SQL中分割字符串并保存到数组中