thaiall logomy background

คำสำคัญ : php short 25

my town
กฤษฎา ตันเปาว์ | จริยธรรมสำหรับผู้บริหาร | วิจัยคืออะไร | ศูนย์สอบ | KM | SWOT | คำสำคัญ
ต.ย.25 ฟังก์ชัน สำหรับการจับเวลาทำงานของเว็บเพจ
โปรแกรมสำหรับบันทึกเวลา เก็บไว้เมื่อส่งค่า start และ หยุดเมื่อส่งค่า stop แล้วหาช่องว่างของเวลา หรือ gap ซึ่งตัวอย่างนี้มี 3 ตัวอย่างย่อย 1) จับเวลาการ delay 5 วินาทีผ่านคำสั่ง for 2) เทียบระหว่าง for กับ while 3) ปรับเพิ่มการใช้ functionให้อ่านโปรแกรมได้ง่ายขึ้น ผลการทดสอบเปรียบเทียบระหว่าง for กับ while พบว่า รอบแรก process ไม่ว่า for หรือ while จะใช้เวลานานกว่า แต่ถ้าเพิ่มคู่ต่อไป อาจเท่ากัน หรือ สลับกันมากกว่า สรุปว่าไม่แน่นอน แต่ใกล้เคียงกัน คาดว่า มีปัจจัยอื่นส่งผลต่อการทำงาน เมื่อทดสอบบนเครื่องบริการ Run PHP แบบ Online ทั้ง 4 อาทิ phptester.net หรือ onlinephpfunctions.com พบว่าให้ผลในการทดสอบของคำสั่ง for และ while ใกล้เคียงกันมาก จึงสรุปได้ว่า ไม่แตกต่างกันตามผลการทดสอบในตัวอย่างที่ 3 แต่ต้องเปลี่ยน <br> เป็น \n เพราะแสดงผลเป็น text ธรรมดา

<?php
$gap = 0;
xTime("start"); // start process
for($i=1;$i<=5;$i++) sleep(1); // Delay 5 seconds
xtime("stop"); // stop process
echo number_format($gap,9) . " seconds"; // 9
// function for [start] and [stop]
function xTime ($action) {
  global $gap;
  list($microsecond,$second) = preg_split('/ /',microtime());  
  // echo $microsecond; // 0.61209100
  if ($action == "start") 
    $gap = $second + $microsecond;  
  else 
    $gap = $second + $microsecond - $gap;
}
?>
Test on PHP 5.6, 7.0 at http://phptester.net/
Gap = 5.000725985 seconds

<?php
srand(time());
/* 1: Main Loop (test for [while] and [for] that swap 100 times) */
$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:
			// Process #1 : for
			xTime("start"); 
			pro1();
			$gap1 += number_format(xTime("stop"),9); // ทศนิยม 9 ตำแหน่ง
			$cnt1++;
		break;
		case 1:
			// Process #2 : while () {}
			xTime("start");
			pro2();
			$gap2 += number_format(xTime("stop"),9);
			$cnt2++;
		break;
		case 2:
			// Process #3 : do {} while();
			xTime("start");
			pro3();
			$gap3 += number_format(xTime("stop"),9);		
			$cnt3++;
		break;
	}
}	
/* 2: Result Display */
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/>";
/* 3: Function area */
function xTime ($action) {
  global $gap;
  list($microsecond,$second) = preg_split("/[ ]/",microtime());  
  if ($action == "start") 
    $gap = $second + $microsecond;  
  else
    $gap = $second + $microsecond - $gap;
  return $gap;
}
/* 3: Function area */
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);
}
?>

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

<?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';
	// $x = '0  1'; // this is different
	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;
}
?>
Dir : php File : indexo.html Topic : php_short_25

Reset | Decode | Y0dodw== | YVc1a1pYaHZMbWgwYld3PQ==
คุณคิดอะไรอยู่
Thaiall.com