การ compare string ด้วย strpos() vs preg_match() อันไหนไวกว่ากัน?
จากที่ google ดูเห็นเขาเทียบความเร็วในการประมวลผลระหว่าง strpos() และ preg_match() ไว้ที่นี่ ผมตัดข้อมูลสั้นๆมาแปะไว้ให้ดูดังนี้
+----------------------------------------------------------------------+
| n=1 2 3 10 100 1000 10000 |
|strpos() 0.01 ms 0.02 ms 0.04 ms 0.2 ms 0.9 ms 2.6 ms 25.6 ms|
|preg_match() 0.2 ms 0.2 ms 0.3 ms 0.47 ms 0.95 ms 7.4 ms 72.2 ms|
|Ratio 1/20 1/10 1/7 1/2 1/1 1/3 1/3 |
+----------------------------------------------------------------------+
หรือดูข้อมูลเต็มได้ ที่นี่
จากตัวเลขดู strpos() จะทำงานได้ไวกว่า แต่ไม่ใช่ว่าจะต้องใช้ strpos() ตลอด ขึ้นอยู่กับงานของเรา บางทีจำเป็นต้องใช้ preg_match() ก็ต้องใช้ หรือถ้ามีการเทียบค่า string ง่ายๆ ไม่ซับซ้อนก็ควรใช้ strpos() ไป
- roteee's blog
- Log in or register to post comments
- 414 reads
Recent comments