CodeIgniter - การเปรียบเทียบ
การตั้งค่า Benchmark Points
หากคุณต้องการวัดเวลาที่ใช้ในการดำเนินการชุดบรรทัดหรือการใช้หน่วยความจำคุณสามารถคำนวณได้โดยใช้จุดเปรียบเทียบใน CodeIgniter มี“ แยกต่างหากBenchmarking” สำหรับจุดประสงค์นี้ใน CodeIgniter
คลาสนี้โหลดโดยอัตโนมัติ คุณไม่ต้องโหลด สามารถใช้ได้ทุกที่ในคลาสคอนโทรลเลอร์มุมมองและโมเดลของคุณ สิ่งที่คุณต้องทำคือทำเครื่องหมายจุดเริ่มต้นและจุดสิ้นสุดจากนั้นเรียกใช้ไฟล์elapsed_time() ฟังก์ชันระหว่างจุดที่ทำเครื่องหมายทั้งสองนี้และคุณจะได้รับเวลาที่ใช้ในการรันโค้ดดังที่แสดงด้านล่าง
<?php 
   $this->benchmark->mark('code_start');
  
   // Some code happens here  
   $this->benchmark->mark('code_end');
  
   echo $this->benchmark->elapsed_time('code_start', 'code_end'); 
?> 
    หากต้องการแสดงการใช้งานหน่วยความจำให้ใช้ฟังก์ชัน memory_usage() ดังแสดงในรหัสต่อไปนี้
<?php 
   echo $this->benchmark->memory_usage(); 
?> 
    ตัวอย่าง
สร้างตัวควบคุมที่เรียกว่า Profiler_controller.php และบันทึกไว้ใน application/controller/Profiler_controller.php
<?php 
   class Profiler_controller extends CI_Controller {
  
      public function index() {
	
         //enable profiler
         $this->output->enable_profiler(TRUE); 
         $this->load->view('test'); 
      } 
  
      public function disable() {
	
         //disable profiler 
         $this->output->enable_profiler(FALSE); 
         $this->load->view('test'); 
      }
		
   } 
?> 
    สร้างไฟล์มุมมองที่เรียกว่า test.php และบันทึกไว้ที่ application/views/test.php
<!DOCTYPE html> 
<html lang = "en">
 
   <head> 
      <meta charset = "utf-8"> 
      <title>CodeIgniter View Example</title> 
   </head>
	
   <body> 
      CodeIgniter View Example 
   </body>
	
</html> 
    เปลี่ยนไฟล์ route.php ที่ application/config/routes.php เพื่อเพิ่มเส้นทางสำหรับคอนโทรลเลอร์ด้านบนและเพิ่มบรรทัดต่อไปนี้ที่ท้ายไฟล์
$route['profiler'] = "Profiler_controller"; 
$route['profiler/disable'] = "Profiler_controller/disable" 
    หลังจากนั้นคุณสามารถพิมพ์ URL ต่อไปนี้ในแถบที่อยู่ของเบราว์เซอร์ของคุณเพื่อดำเนินการตามตัวอย่าง
http://yoursite.com/index.php/profiler 
    URL ด้านบนจะเปิดใช้งานตัวสร้างโปรไฟล์และจะสร้างผลลัพธ์ดังที่แสดงในภาพหน้าจอต่อไปนี้
                หากต้องการปิดใช้งานการสร้างโปรไฟล์ให้เรียกใช้ URL ต่อไปนี้
http://yoursite.com/index.php/profiler/disable