dousu1916 2012-04-11 17:05
浏览 6
已采纳

PHP中的代码循环优化

Is there any way to optimize this piece of code to work faster? I'd appreciate any suggestions!

This piece of code processes the transferring of edges during graph creation.

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

example of variables:

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)
  • 写回答

2条回答 默认 最新

  • dqkx69935 2012-04-11 21:42
    关注

    Thank you all! The reason of slow speed was coz of using functions strtotime() and date().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错