微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在PHP中迭代日期时的效率

此应用程序的目的是以最有效的方式在车间中调度大量机器.该过程是递归的,需要制定计划表并衡量效率.这可行,但实际上需要几天的时间.下面的代码块是一个很大的时间消耗:

foreach($machines AS $machine) {
# To begin, we analyze the schedule thus far to get this machine's existing schedule.
    $machSched = array();
    foreach($schedule AS $booking) {
        if($booking['mach']==$machine && strtotime($booking['end']) > date('U')) {
            $machSched[] = $booking;
        }
    }
    # We seek the next time the machine can be booked.  We begin by sorting its current bookings.
    aasort($machSched, 'start');
    # Now we construct the list of available times
    $lastEnd = date('U');
    $freeTimes=array();
    foreach($machSched AS $booking) {
        if(strtotime($booking['start']) > $lastEnd) $freeTimes[] = array('start' => $lastEnd, 'end' => strtotime($booking['start']));
        $lastEnd = strtotime($booking['end']);
    }
    $freeTimes[] = array('start' => $lastEnd, 'end' => strtotime('2030-12-31'));
    # Now we go through each available timeslot to see what we can book.
    foreach($freeTimes AS $slot) {
                   // Scheduling stuff here...
    }
}

该块遍历每台计算机的现有计划时间,对其进行排序,并创建“空闲插槽”数组(现有计划项目之间的时间.我已经优化和优化了该程序,但似乎无法使用请注意aasort是通过关联数组中的键对关联数组数组进行排序的函数.

任何帮助,将不胜感激!

解决方法:

如果您知道日期存储的格式,则应该使用strptime而不是strtotime.使用strtotime意味着每个调用都必须独立计算日期的格式,因此您必须在每个循环中考虑该诊断.这可能是一个很大的差异.

我的简单基准测试表明,time()大约比date(‘U’)快一个数量级,而strptime()的速度比strtotime()快5倍.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐