写这些多个elseif的速度更快?

时间:2013-06-21 23:33:19

标签: php

有没有办法更简单,更快地写这个?

$currTime = date('H:i');
if ($currTime >= "12:09" && $currTime <= "16:08") {
    echo rand(260, 272);
} elseif ($currTime >= "16:09" && $currTime <= "18:08") {
    echo rand(278, 286);
} elseif ($currTime >= "18:09" && $currTime <= "20:08") {
    echo rand(293, 303);
} elseif ($currTime >= "20:09" && $currTime <= "23:38") {
    echo rand(338, 359);
} elseif ($currTime >= "23:39" && $currTime <= "23:59") {
    echo rand(293, 302);
} elseif ($currTime >= "01:06" && $currTime <= "02:08") {
    echo rand(195, 210);
} elseif ($currTime >= "02:09" && $currTime <= "02:23") {
    echo rand(168, 179);
} elseif ($currTime >= "02:24" && $currTime <= "07:08") {
    echo rand(121, 128);
} elseif ($currTime >= "07:09" && $currTime <= "09:08") {
    echo rand(143, 160);
} elseif ($currTime >= "09:09" && $currTime <= "12:08") {
    echo rand(187, 207);
} else {
    echo rand(233, 241);
}

6 个答案:

答案 0 :(得分:3)

$currTime = date('H:i');

$timevals=Array(
"23:39" => Array(293,302),
"20:09" => Array(338,359),
"18:09" => Array(293,303),
//... (etc, descending order)
);

foreach($timevals AS $time => $vals) {
  if($curtime >=$time) {
    echo rand($vals[0],$vals[1]);
    break;
  }
}

答案 1 :(得分:3)

不要以更快的方式看......以正确的方式看......

  • 永远不要比较datetime strings
  • 使用mt_rand代替rand
  • 使用functionsobject而非长时间切换或if else

示例:

$currTime = new DateTime();

$range = [
        new TimeRange("12:09-16:08", "260-272"),
        new TimeRange("16:09-18:08", "278-286"),
        new TimeRange("18:09-20:08", "293-303"),
        new TimeRange("20:09-23:38", "338-359"),
        new TimeRange("23:39-23:59", "195-210"),
        new TimeRange("01:06-02:23", "168-179"),
        new TimeRange("02:24-07:08", "121-128"),
        new TimeRange("07:09-09:08", "143-160"),
        new TimeRange("09:09-12:08", "187-241")
];

foreach($range as $timeRange) {
    if ($timeRange->inRange($currTime)) {
        printf("Current Time\t: %s\n", $currTime->format("H:i"));
        printf("Range Time\t: %s\n", $timeRange->getRange());
        printf("Random Value\t: %s\n", $timeRange->getRandom());
        break;
    }
}

输出

Current Time    : 01:53
Range Time      : 01:06 - 02:23
Random Value    : 168

使用过的课程

class TimeRange {
    private $timeFrom, $timeTo;
    private $numFrom, $numTo;
    private $format;

    function __construct($time, $number, $format = "H:i") {
        list($timeFrom, $timeTo) = explode("-", $time);
        list($this->numFrom, $this->numTo) = explode("-", $number);
        $this->timeFrom = DateTime::createFromFormat($format, $timeFrom);
        $this->timeTo = DateTime::createFromFormat($format, $timeTo);
        $this->format = $format;
    }

    function inRange(DateTime $currTime) {
        return $currTime >= $this->timeFrom && $currTime <= $this->timeTo;
    }

    function getRandom() {
        return mt_rand($this->numFrom, $this->numTo);
    }

    function getRange() {
        return sprintf("%s - %s", $this->timeFrom->format($this->format), $this->timeTo->format($this->format));
    }
}

答案 2 :(得分:0)

没有更简洁的方法,在这种情况下,开关语句将不起作用。你所能做的只是让你的if更短,但就是这样。我认为你在这种情况下的做法已经足够了。

答案 3 :(得分:0)

采取我的解决方案)这缩短并简化了代码..

<?
function plan($a, $b, $c, $d)
{
    $t = date('H:i');
    if ($a <= $t && $t <= $b) { echo rand($c, $d); }
}
plan("12:09", "16:08", 260, 272);
plan("16:09", "18:08", 278, 286);
plan("18:09", "20:08", 293, 303);
plan("20:09", "23:38", 338, 359);
plan("23:39", "23:59", 293, 302);
plan("00:00", "01:05", 233, 241);
plan("01:06", "02:08", 195, 210);
plan("02:09", "02:23", 168, 179);
plan("02:24", "07:08", 121, 128);
plan("07:09", "09:08", 143, 160);
plan("09:09", "12:08", 187, 207);
?>

答案 4 :(得分:0)

检查http://www.phpbench.com/

控制结构

switch/case/default VS. if/elseif/else

Control Structures               | Total time
---------------------------------|-----------
if and elseif (using ==)         | 190 µs
if, elseif and else (using ==)   | 189 µs
if and elseif (using ===)        | 158 µs
if, elseif and else (using ===)  | 170 µs
switch / case                    | 200 µs
switch / case / default`         | 228 µs

<强>结论:

使用switch/caseif/elseif 几乎相同注意该测试未确定===(完全等于),并且稍微快于使用==(等于)。

答案 5 :(得分:0)

$currTime = date('H:i');


$arr=array

(
"12:09,16:08"=>'260,272',
"16:09,18:08"=>'278, 286',
"18:09,20:08"=>'293, 303',
"20:09,23:38"=>'338, 359',
"23:39,23:59"=>'293, 302',
"01:06,02:08"=>'195, 210',
"00:00,00:30" =>'1,10'
//etc,etc...

);

foreach ($arr as $key=>$value) {



$key=explode(',',$key);
$value=explode(',',$value);

if( $currTime>= $key[0] && $currTime<=$key[1]) {
echo  rand($value[0],$value[1]);    

}




}


...however, i would keep OP's code as it is... :)