PHP中的代码循环优化

PHP中的代码循环优化,php,optimization,Php,Optimization,有没有办法优化这段代码以更快地工作?如果有任何建议,我将不胜感激 这段代码在图形创建过程中处理边的传递 foreach($times_arrival as $city_id => $time_points) { // if city is not prohibited for transfers and there is and exists any departure times for this city if (isset($times_departure[$city_id]) &a

有没有办法优化这段代码以更快地工作?如果有任何建议,我将不胜感激

这段代码在图形创建过程中处理边的传递

foreach($times_arrival as $city_id => $time_points) {
// if city is not prohibited for transfers and there is and exists any departure times for this city
if (isset($times_departure[$city_id]) && isset($cities[$city_id]))
{
    foreach($times_arrival[$city_id] as $t1_info) 
    {
        foreach($times_departure[$city_id] as $t2_info) 
        {
            if ($t1_info[0] != $t2_info[0]) //transfers are allowed only for different passages
            {
                $t1 = $t1_info[1];
                $t2 = $t2_info[1];

                $vertex_key = new Vertex($city_id, $t1, 1);
                $vertex_key = $vertex_key->toString();

                //minimum transfer time is 10 min.
                if (date('H:i', strtotime($t2)) > date('H:i', strtotime('+ 10 minutes', strtotime($t1))))
                {
                    $this->graph[$vertex_key][] = new Edge(
                        NULL,
                        $vertex_key, 
                        new Vertex($city_id, $t2, 0),
                        (float) 0,
                        $f((strtotime($t2) - strtotime($t1)) / 60, 0, 1) //edge weight
                    );
                }
                //if transfer is on the bound of the twenty-four hours
                else if (date('H:i', strtotime('+ 24 hours', strtotime($t2))) > date('H:i', strtotime('+ 10 minutes', strtotime($t1))))
                {
                    $this->graph[$vertex_key][] = new Edge(
                        NULL, 
                        $vertex_key,
                        new Vertex($city_id, $t2, 0),
                        (float) 0,
                        $f(strtotime('+ 24 hours', strtotime($t2)) - strtotime($t1) / 60, 0, 1) //edge weight
                    );
                }
            }
        }
    }
}
}
变量示例:

var_dump($times_arrival); //$times_departure have the same structure
array
  3 => 
    array
      0 => 
        array
          0 => string '1' (length=1)
          1 => string '08:12' (length=5)
      1 => 
        array
          0 => string '2' (length=1)
          1 => string '08:40' (length=5)
  41 => 
    array
      0 => 
        array
          0 => string '21' (length=2)
          1 => string '12:40' (length=5)

在这种情况下,你只能说你选择了一个好的还是坏的算法。在我看来,你的代码没有额外的计算

只有一个建议-如果可能的话,使用Xdebug分析代码并找出瓶颈所在。

谢谢大家!
速度慢的原因是因为使用了函数
strotime()
date()

Wow
foreach(foreach(foreach($foo=>$bar))
请发布一些
$times\u-arrival
数据。这部分代码需要多长时间,数组有多大?您确定脚本的总时间主要由该循环占用吗?例如,这需要0.02秒:我看到的第一件事是您可以将一个strottime(“+24小时”,strottime($t2))替换为strottime($t2)+86400,作为一般建议,您应该对代码进行注释,以便人们能够更轻松地了解您所做的事情。至少他可能能够在他的节点上进行预处理,这将使他的预期大O从~O(n^3)降低到O(n^2),或者可能是O(nlogn),具体取决于他必须做什么。现在是O(n^2)而不是n^3,并且可能会将其降低到O(nlogn)或者不是我和你说的“取决于他到底要做什么”。但它不是干净的数学-它的程序和可能最终可能通过优化代码而不是算法来加速。从技术上讲,它是O(m*n^2),其中n是到达阵列的长度,m是离开阵列的长度。我们之所以知道这一点,是因为有三个嵌套for循环。我保证有一个更快的算法来实现这一点,如果他可以发布关于他的数据输入的澄清。