Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > มาเขียน PHP ให้ทำงานอย่างประสิทธิภาพ (Performance) กันเถอะ (ตอนที่ 3)



 
Clound SSD Virtual Server

มาเขียน PHP ให้ทำงานอย่างประสิทธิภาพ (Performance) กันเถอะ (ตอนที่ 3)



ติดตามบทความล่าสุดของผู้เขียนได้ที่ phpinfo() Facebook Page



มาเขียน PHP ให้ทำงานอย่างประสิทธิภาพ (Performance) กันเถอะ (ตอนที่ 3) บทความนี้นำเสนอสิ่งที่ควรรู้เกี่ยวกับภาษา PHP ทั้งสิ่งที่ควรใช้ และไม่ควรใช้ ที่จะทำให้โปรแกรมทำงานได้เร็วขึ้น




11. ใช้ isset() แทน count() กับ indexed array หรือ string ในกรณีที่ต้องการตรวจสอบจำนวนสมาชิก หรือจำนวนตัวอักษร ว่าเกินกว่าที่กำหนดไว้หรือไม่

สมมติเรามี indexed array ดังต่อไปนี้

$arr = array(0, 1, 2, 3, 4, 5);


แล้วเราต้องการจะตรวจสอบว่า จำนวนสมาชิกใน array นี้ มีจำนวนมากกว่าหรือน้อยกว่าหรือเท่ากับที่กำหนดหรือไม่ โดยปกติเราจะใช้ count()

if (count($arr) > 5) { // do something }


ในกรณีนี้ เราสามารถใช้ isset() แทนได้ ซึ่งความเร็วจะมากกว่าการใช้ count() มากๆ เพราะไม่มีการเรียกใช้ฟังก์ชั่น

// ตรวจว่ามีสมาชิกในตำแหน่งที่ 5 แล้วหรือยัง (สมาชิกตัวที่ 6) // เพราะ indexed array ใน PHP สมาชิกตัวแรกจะเริ่มต้นที่ 0 (ถ้าไม่กำหนดเป็นอย่างอื่น) // ประโยคนี้จะเท่ากับ if (count($arr) > 5) if (isset($arr[5])) { // do something }


แต่ต้องระวังด้วยว่า การใช้ isset() เพื่อตรวจสอบความยาวของ array นั้น ต้องเป็น indexed array ที่มี key ต่อเนื่องกันเสมอ

$arr = array(0, 1, 2); $arr[10] = 555; $arr[11] = 666; $arr[12] = 777; // มาถึงตรงนี้ $arr จะมีสมาชิก 6 ตัว แต่ key ไม่เรียงกัน // ซึ่งจะทำให้เงื่อนไขตรงนี้ไม่เป็นจริง เพราะไม่มีสมาชิกที่มี key เท่ากับ 5 if (isset($arr[5])) { // do something }


เราสามารถใช้เทคนิคนี้กับ string ได้เช่นเดียวกัน

$str = 'Hello World'; // ประโยคนี้จะเท่ากับ if (strlen($arr) > 5) if (isset($str[5])) { echo "Error: string is too long"; }


แต่ในกรณีของ string ต้องเป็น string ที่เป็น encoding ชนิด single byte เท่านั้น
หากเป็นพวก UTF-8 จะไม่ได้ผลตามที่ต้องการ


หาก string ที่จะตรวจสอบมี encoding เป็นชนิด UTF-8
$str = 'สวัสดีจ้า'; // เห็นว่ายาวแค่ 10 ตัวอักษรก็จริง // แต่เงื่อนไขข้างล่างจะเป็นจริงเสมอ // เพราะภาษาไทยใช้ 3 bytes ต่อ 1 ตัวอักษร ดังนั้น 'สวัสดีจ้า' จะมีความยาว 30 ตัวอักษร (30 bytes) if (isset($str[20])) { echo "Error: string is too long"; }


ดังนั้นการตรวจความยาวของ multibyte string ต้องใช้ฟังก์ชั่นอื่นๆ ช่วย ไม่สามารถใช้ isset() ได้

$str = 'สวัสดีจ้า'; if (mb_strlen($str, 'UTF-8') > 10) { echo "Error: string is too long"; }





12. ใช้ isset() แทน in_array()

ในกรณีที่เราต้องการตรวจสอบว่า มีค่าอยู่ใน array หรือไม่ เรามักจะใช้ in_array()

$types = array('bool', 'int', 'float', 'string', 'array'); if (in_array($value, $types)) { // do something }


ซึ่งหากต้องมีการตรวจสอบอะไรแบบนี้เป็นจำนวนหลายๆ ครั้ง (เช่นในลูป) จะเสียเวลามากๆ เพราะ in_array() เป็นฟังก์ชั่น และมีการทำงานในลักษณะของการวนลูปตรวจสอบค่าของสมาชิกทุกตัวใน array

ในกรณีนี้ เราสามารถใช้ associative array ร่วมกับ isset() ได้
คือแทนที่เราจะตรวจสอบว่า "มีค่าอยู่ใน array หรือไม่" เปลี่ยนเป็น "มี key อยู่ใน array หรือไม่" แทน


เร็วกว่ามากๆ
$types = array( 'bool' => true, 'int' => true, 'float' => true, 'string' => true, 'array' => true, ); if (isset($types[$value])) { // do something }


ลองทดสอบความเร็วดู
<?php $types = array('bool', 'int', 'float', 'string', 'array'); $t = microtime(true); for ($i = 0; $i < 1000000; ++$i) { if (in_array($i, $types)) { } } $time['in_array()'] = microtime(true) - $t; $types = array( 'bool' => true, 'int' => true, 'float' => true, 'string' => true, 'array' => true, ); $t = microtime(true); for ($i = 0; $i < 1000000; ++$i) { if (isset($types[$i])) { } } $time['isset()'] = microtime(true) - $t; print_r($time);


Array ( [in_array()] => 0.671875 [isset()] => 0.125 )


จะเห็นว่าการใช้ isset() จะเร็วกว่า in_array() ประมาณ 5 เท่า




13. ใช้ if หลายๆ ครั้ง แทนที่จะใช้ || ในกรณีที่มีการตรวจสอบเงื่อนไขนั้นบ่อยๆ (เช่นในลูป) และผลของเงื่อนไขนั้นคือการ return, break หรือ continue

ลองพิจารณาโค้ดต่อไปนี้


function abc($a, $b, $c) { // หากทั้งสามตัวแปร ตัวใดตัวหนึ่งให้ผลเป็น false ให้ออกจากฟังก์ชั่น if (!$a || !$b || !$c) { return; } // do something }


ดูเผินๆ อาจจะไม่มีอะไร แต่เราสามารถทำให้โค้ดทำงานได้เร็วกว่านั้นด้วยการไม่ใช้ ||

function abc($a, $b, $c) { // หากทั้งสามตัวแปร ตัวใดตัวหนึ่งให้ผลเป็น false ให้ออกจากฟังก์ชั่น if (!$a) { return; } if (!$b) { return; } if (!$c) { return; } // do something }


ในแบบหลังจะทำงานเร็วกว่า เพราะ PHP ไม่มีความจำเป็นต้องสร้าง expression stack และจะทำงานเร็วที่สุดในกรณีที่เงื่อนไขตัวแรกเป็นจริง ($a)
แต่อย่างไรก็ตาม ในกรณีที่ใช้ในฟังก์ชั่นแบบนี้ ความเร็วจะเพิ่มขึ้นเพียงนิดเดียวจนแทบไม่เห็นความแตกต่าง แล้วเทคนิคนี้มีประโยชน์อย่างไร

ลองพิจารณาโค้ดต่อไปนี้


foreach ($items as $item) { // หากสมาชิกตัวใดตัวหนึ่งของ $item ให้ผลเป็น false ให้ทำงานรอบถัดไปทันที if (empty($item['a']) || empty($item['b']) || empty($item['c'])) { continue; } // do something }


จากตัวอย่างดังกล่าวจะเห็นว่ามีการทำงานในลูป ซึ่งหากมีจำนวนครั้งของลูปมาก PHP ก็จะต้องสร้าง expression stack ตามจำนวนครั้ง

แบบนี้จะเร็วกว่า
foreach ($items as $item) { // หากสมาชิกตัวใดตัวหนึ่งของ $item ให้ผลเป็น false ให้ทำงานรอบถัดไปทันที if (empty($item['a'])) { continue; } if (empty($item['b'])) { continue; } if (empty($item['c'])) { continue; } // do something }


เทคนิคนี้จะมีประโยชน์มากในโปรแกรมที่ต้องการความเร็วสูงสุดเท่าที่จะเป็นไปได้ เช่นโปรแกรมประเภท parser หรือ compiler
แม้จะต้องเขียนโค้ดยาวกว่าแต่ก็คุ้มค่าที่จะใช้ และทำให้โค้ดอ่านเข้าใจง่ายขึ้นด้วย
และเทคนิคนี้ใช้ได้กับทุกภาษาโปรแกรม เพราะไม่ว่าภาษาใดก็ตาม เงื่อนไขจำนวนมากทำงานช้ากว่าเงื่อนไขเดี่ยวๆ เสมอ


ทดสอบความเร็ว
<?php $a = 1; $b = 1; $c = 1; $t = microtime(true); for ($i = 0; $i < 1000000; ++$i) { if (empty($a) || empty($b) || empty($c)) { continue; } } $time['||'] = microtime(true) - $t; $t = microtime(true); for ($i = 0; $i < 1000000; ++$i) { if (empty($a)) { continue; } if (empty($b)) { continue; } if (empty($c)) { continue; } } $time['multiple if'] = microtime(true) - $t; print_r($time);


ผลลัพธ์
Array ( [||] => 0.1875 [multiple if] => 0.15625 )


จะเห็นว่าการใช้ if หลายๆ ครั้งเร็วกว่าเล็กน้อย แม้จะไม่มีเงื่อนไขที่เป็นจริงเกิดขึ้นเลยก็ตาม




14. อย่าใช้ฟังก์ชั่นประเภท xxxval() แต่ใช้ casting operator แทน

ฟังก์ชั่นต่อไปนี้

intval()
floatval()
strval()
boolval() (PHP5.5)

เป็นฟังก์ชั่นที่ใช้แปลงค่าให้เป็นชนิดที่ต้องการ
แต่เนื่องด้วยมันเป็นฟังก์ชั่น มันจึงทำงานช้า
ซึ่งการทำงานของมันสามารถทดแทนด้วย casting operator ได้แก่

(int)
(float)
(string)
(bool)


echo (int)'123.555'; echo (float)'123.555'; echo (string)'123.555'; echo (bool)'123.555';


ทดสอบความเร็ว
<?php $t = microtime(true); for ($i = 0; $i < 1000000; ++$i) { $a = strval($i); } $time['strval()'] = microtime(true) - $t; $t = microtime(true); for ($i = 0; $i < 1000000; ++$i) { $a = (string)$i; } $time['(string)'] = microtime(true) - $t; print_r($time);


ผลลัพธ์
Array ( [strval()] => 0.8125 [(string)] => 0.4375 )


จะเห็นว่า casting operator ทำงานเร็วกว่าเท่าตัว




15. อย่าใช้ function ที่เป็น alias ของอีกฟังก์ชั่น

PHP มีฟังก์ชั่นเป็นจำนวนมาก ที่เป็น alias ของอีกฟังก์ชั่นหนึ่ง (ชื่อต่างกัน แต่ทำงานเหมือนกัน) เช่น

join() = implode()
sizeof() = count()
pos() = current()
chop() = trim()
fputs() = fwrite()
session_commit() = session_write_close()

ซึ่งความเร็วของมันส่วนใหญ่จะช้ากว่าเรียกใช้ฟังก์ชั่นตัวจริง

ทดสอบความเร็ว
$arr = array(1, 2, 3); $t = microtime(true); for ($i = 0; $i < 1000000; ++$i) { $n = sizeof($arr); } $time['sizeof()'] = microtime(true) - $t; $t = microtime(true); for ($i = 0; $i < 1000000; ++$i) { $n = count($arr); } $time['count()'] = microtime(true) - $t; print_r($time);


ผลลัพธ์
Array ( [sizeof()] => 0.390625 [count()] => 0.375 )





บทความที่เกี่ยวข้อง
มาเขียน PHP ให้ทำงานอย่างประสิทธิภาพ (Performance) กันเถอะ (ตอนที่ 1)
มาเขียน PHP ให้ทำงานอย่างประสิทธิภาพ (Performance) กันเถอะ (ตอนที่ 2)



ติดตามบทความล่าสุดของผู้เขียนได้ที่ phpinfo() Facebook Page







   
Share
Bookmark.   

  By : phpinfo()
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2013-08-23
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่