1 <?php
2 function get_time(){
3 return array_sum(explode(' ', microtime()));
4 }
5
6 // && vs || execution
7 $time = get_time();
8 for($i=1;$i<=1000000;$i++){
9 (isset($_POST['test']) && $test = $_POST['test'])
10 || (isset($_GET['test']) && $test = $_GET['test'])
11 || $test = 'test';
12 }
13 echo '&& vs || execution: '. (get_time()-$time).'<br />';
14
15 // Normal execution
16 $time = get_time();
17 for($i=1;$i<=1000000;$i++){
18 if (isset($_POST['test']) && !empty($_POST['test']))
19 $test = $_POST['test'];
20 elseif (isset($_GET['test']) && !empty($_GET['test']))
21 $test = $_GET['test'];
22 else
23 $test = 'test';
24 }
25 echo 'Normal execution: '. (get_time()-$time).'<br />';
当 $_POST['test'] 与 $_GET['test'] 无值时:
0.7159481048584
0.65181183815002
-
0.71318793296814
0.6400249004364
-
0.71299386024475
0.64203190803528
平均---------------------
0.714043299
0.644622882
当$_POST['test'] 或 $_GET['test'] 有值时:
0.8075578212738
0.90688800811768
-
0.79875206947327
0.89607310295105
-
0.81095600128174
0.89360904693604
平均---------------------
0.805755297
0.898856719