<!-- sample 1 -->
<?php
$gap = 0;
xTime(
"start"
);
for
($i=1;$i<=5;$i++) sleep(1);
xtime(
"stop"
);
echo number_format($gap,9) .
" seconds"
;
function
xTime ($action) {
global $gap;
list($microsecond,$second) = preg_split(
'/ /'
,microtime());
if
($action ==
"start"
)
$gap = $second + $microsecond;
else
$gap = $second + $microsecond - $gap;
}
?>
Test on PHP 5.6, 7.0 at http:
Gap = 5.000725985 seconds
<!-- sample 2 -->
<?php
srand(time());
$gap = 0;
$gap1 = 0; $gap2 = 0; $gap3 = 0;
$cnt1 = 0; $cnt2 = 0; $cnt3 = 0;
for
($num=1;$num<=3000000;$num++){
$option = rand() % 3;
switch
($option){
case
0:
xTime(
"start"
);
pro1();
$gap1 += number_format(xTime(
"stop"
),9);
$cnt1++;
break
;
case
1:
xTime(
"start"
);
pro2();
$gap2 += number_format(xTime(
"stop"
),9);
$cnt2++;
break
;
case
2:
xTime(
"start"
);
pro3();
$gap3 += number_format(xTime(
"stop"
),9);
$cnt3++;
break
;
}
}
echo
"Time of for(){} : $gap1 seconds with $cnt1 times<br/>"
;
echo
"Time of while(){} : $gap2 seconds with $cnt2 times<br/>"
;
echo
"Time of do{} while() : $gap3 seconds with $cnt3 times<br/>"
;
function
xTime ($action) {
global $gap;
list($microsecond,$second) = preg_split(
"/[ ]/"
,microtime());
if
($action ==
"start"
)
$gap = $second + $microsecond;
else
$gap = $second + $microsecond - $gap;
return
$gap;
}
function
pro1(){
for
($i=1;$i<=10;$i++) { } }
function
pro2(){ $j=0;
while
($j < 10) { $j++; } }
function
pro3(){ $k=0;
do
{ $k++; }
while
($k < 10);
}
?>
<!-- sample 3 -->
<?php
srand(time());
$gap1=0; $gap2=0;
$cnt1=0; $cnt2=0;
for
($num=1;$num<=1000000;$num++){
if
(rand() % 2 == 0)
$gap1 += xTime(process1(start()));
else
$gap2 += xTime(process2(start()));
}
echo
"Time of for : $gap1 microseconds with $cnt1 times<br/>"
;
echo
"Time of while : $gap2 microseconds with $cnt2 times<br/>"
;
function
process1 ($start) {
global $cnt1;
$cnt1++;
for
($i=1;$i<=100;$i++) { }
return
$start;
}
function
process2 ($start) {
global $cnt2;
$cnt2++;
$j=0;
while
($j < 100) { $j++; }
return
$start;
}
function
start() {
list($microsecond,$second) = preg_split(
"/[ ]/"
,microtime());
$start = $second + $microsecond;
return
$start;
}
function
xTime ($start) {
list($microsecond,$second) = preg_split(
"/[ ]/"
,microtime());
return
number_format($second + $microsecond - $start,9);
}
?>
<!-- sample 4 -->
<?php
srand(time());
$gap1=0; $gap2=0;
for
($num=1;$num<=1000000;$num++){
if
(rand() % 2 == 0)
$gap1 += process1(start());
else
$gap2 += process2(start());
}
echo
"Time of '' : $gap1 microseconds<br/>"
;
echo
'Time of "" :'
.
" $gap2 microseconds<br/>"
;
function
process1 ($start) {
$x =
'0 '
. $start .
' 1'
;
list($microsecond,$second) = preg_split(
"/[ ]/"
,microtime());
return
number_format($second + $microsecond - $start,9);
}
function
process2 ($start) {
$x =
"0 $start 1"
;
list($microsecond,$second) = preg_split(
"/[ ]/"
,microtime());
return
number_format($second + $microsecond - $start,9);
}
function
start() {
list($microsecond,$second) = preg_split(
"/[ ]/"
,microtime());
$start = $second + $microsecond;
return
$start;
}
?>