By multiplex on Tag:

array & switch ของ php

แบบไหนทำงานเร็วกว่ากันครับ เช่น

function thai_month($mm) {
$r_month= array( "01" => 'มกราคม',
"02" => 'กุมภาพันธ์',
"03" => 'มีนาคม',
"04" => 'เมษายน',
"05" => 'พฤษภาคม',
"06" => 'มิถุนายน',
"07" => 'กรกฎาคม',
"08" => 'สิงหาคม',
"09" => 'กันยายน',
"10" => 'ตุลาคม',
"11" => 'พฤศจิกายน',
"12" => 'ธันวาคม' );
return $r_month= [$mm];
}

function thai_month($mm) {
switch($mm) {
case '01' : $t_month = "มกราคม"; break;
case '02' : $month = "กุมภาพันธ์";break;
case '03' : $month = "มีนาคม";break;
case '04' : $month = "เมษายน";break;
case '05' : $month = "พฤษภาคม";break;
case '06' : $month = "มิถุนายน";break;
case '07' : $month = "กรกฎาคม";break;
case '08' : $month = "สิงหาคม";break;
case '09' : $month = "กันยายน";break;
case '10' : $month = "ตุลาคม";break;
case '11' : $month = "พฤศจิกายน";break;
case '12' : $month = "ธันวาคม";break;
}
return $month;
}

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

ถ้าแบบนี้เทียบกับแบบแรกอันไหนเร็วกว่ากัครับ
function thai_month($mm) {
switch($mm) {
case '01' : return "มกราคม";
case '02' : return "กุมภาพันธ์";
case '03' : return "มีนาคม";
case '04' : return "เมษายน";
case '05' : return "พฤษภาคม";
case '06' : return "มิถุนายน";
case '07' : return "กรกฎาคม";
case '08' : return "สิงหาคม";
case '09' : return "กันยายน";
case '10' : return "ตุลาคม";
case '11' : return "พฤศจิกายน";
case '12' : return "ธันวาคม";
}
return;
}

ยังไงก็แบบแรกอยู่ดีครับ
เทียบชุดคำสั่งแล้ว worse case ของแบบที่สองคือตรวจสอบทั้งหมด 12 ครั้ง

ส่วนแบบแรก ไม่มีการตรวจสอบ assign ค่าแล้ว jump ไปที่ข้อมูลที่ต้องการเลย

หลายๆ ครั้งฟังก์ชั่น hash มันก็ช้าได้เรื่องนะครับ ยิ่งข้อมูลน้อยๆ อย่างนี้โอกาสที่จะเร็วกว่า hash มีสูง

คุ้นๆว่า switch case มันจะไม่เหมือนกับ if/else if พอสมควรเลยนะครับ เหมือนว่าถ้าเป็น switch case ปกติมันจะทำการเทียบทีเดียวแล้ว jump ไป instruction นั้นๆได้เลย แต่ว่าตัว if/else if ต่างหากที่กรณีที่แย่ที่สุดจะทำ 12 ครั้ง

แต่นั่นก็ต้องขึ้นกับว่าตัว interpreter ทำงานแบบไหนด้วยล่ะมั้งครับ

ขอบคุณครับ งั้นคงต้องไปไล่แก้ code ในลักษณะแบบนี้ใหม่ครับ
แลกกับความเร็ว แล้วเสียหน่วยความจำไปแว๊บหนึ่งประมาณนี้หรือป่าวครับ

ผมว่ากรณีนี้ switch-case (แบบที่สอง) ชนะนะครับ เพราะ range ค่อนข้างแคบ (01-12) optimizer คง jump ที่ระดับ machine code แบบที่ @wiennat บอกไว้ ("เดา"ว่า optimizer ของ interpreter/compiler ส่วนใหญ่ทำแบบนี้รวมถึง php)

ส่วนแบบ array (แบบแรก) ต้องใช้เวลาเพิ่มตอนแปลง index โดยเฉพาะทุกครั้งที่เข้าฟังก์ชัน ต้องมีการสร้าง array และ assign ค่าให้ $r_month ทุกครั้ง (แต่ถึงทำเป็น static variable ไว้ด้านนอกฟังก์ชันก็คิดว่าไม่น่าจะเร็วกว่าอยู่ดีครับ)

แต่ถ้า range กว้างๆ (เช่น 1, 1000, 10000, 100000, ....., 10000000) และมีจำนวนไม่เยอะนี่ เท่าที่เคยทราบบาง optimizer จะทำแบบเดียวกับ if-else แต่ถ้าจำนวนเยอะมากๆอาจใช้ binary-search ครับ

เรื่องความเร็วแบบแรกครับ เพราะใช้ key hash เข้าถึงข้อมูลเลย
ถ้าเรื่องประหยัด memory แบบสองครับเพราะไม่ต้องบันทึกข้อมูลเดือนลง memony ก่อนครับ