繁体   English   中英

写这些多重elseif的更快方法?

[英]Faster way to write these multiple elseif?

有没有一种更简单,更快捷的方式来编写此代码?

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

不要看更快的方法,要看正确的方法。

  • 永远不要像strings一样比较datetime
  • 使用mt_rand而不是rand
  • 使用functionsobject而不要长时间切换,否则

范例:

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

没有更干净的方法可以执行此操作,在这种情况下, switch语句将无法工作。 您所能做的就是使if短一些,仅此而已。 我认为您在这种情况下的方法足够好。

以我的解决方案为准)这可以缩短并简化代码。

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

检查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 几乎相同 请注意 ,该测试不会执行=== (完全等于),并且比使用== (等于)要快一些。

$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... :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM