PHP搜索阵列速度

时间:2015-03-11 10:23:01

标签: 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

1 个答案:

答案 0 :(得分:2)

根据您的评论,日期列包含唯一值。 由于您只对日期列进行过滤,因此通过日期列对数组进行索引的效率要高得多,因此您应该重新构建数据,如下所示:

$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]访问数据来立即获取数据。