Sau d�y m�nh xin chia s? c�ch m� m�nh hay l�m v?i php c?a m�nh , M?i code c?n ph?i t?i uu ngay t? khi vi?t, truy v?n khoa h?c , code chu?n , cache n?u c� th? v� sau d�y l� m?t v�i th? thu?t kh�c n?a:
1. Bi?n
- Khai b�o kh?i t?o bi?n d?nh ki?u nhanh hon 376% so v?i kh�ng d?nh ki?u.
- H?ng ch?m hon 146% so v?i bi?n.
- Bi?n c?c b? nhanh hon 9.9% bi?n to�n c?c.
2. H�m l�m vi?c v?i chu?i
- 'string' nhan hon 0.26% so v?i "string"
- "String" nhanh hon 4% so v?i c� ph�p HEREDOC
- "Stringn" nhanh hon 108% so v?i 'String'."n"
- 'String'.$var nhanh hon 28% so v?i "String$var"
- 'string '.$var.' string' nhanh hon 55% so v?i sprintf('string %s string', $var)
- "n" nhanh hon 70% so v?i chr(10)
- strnatcmp() nhanh hon 4.95% so v?i strcmp()
- strcasecmp() nhanh hon 45% so v?i preg_match()
- strcasecmp() nhanh hon 6.6% so v?i strtoupper($string) == "STRING"
- strcasecmp() nhanh hon 13% so v?i strnatcasecmp()
- strtr($string, $string1, $string2) nhanh hon 10% so v?i str_replace()
- str_replace() nhanh hon 161% so v?i strtr($string, $array)
- stristr() nhanh hon 10% so v?i stripos()
- strpos() nhanh hon 9.7% so v?i strstr()
- isset($str{5}) nhanh hon 176% so v?i strlen($str) > 5
- str_replace($str, $str, $str) hai l?n nhanh hon 17% so v?i str_replace(array, array, string)
- list() = explode() nhanh hon 13% so v?i substr($str, strpos($str))
3. H�m l�m vi?c v?i s?
- ++$int nhanh hon 10% so v?i $int++
- (float) nhanh hon 48% so v?i settype($var, 'float')
4. H�m l�m vi?c v?i m?ng
- list() = $array; nhanh hon 3.4% so v?i g�n t?ng gi� tr?
- in_array() nhanh hon 6% so v?i array_search
- isset($array[$key]) nhanh hon 230% so v?i array_key_exists()
- !empty($array) nhanh hon 66% so v?i count($array)
5. H�m xu?t
- echo nhanh hon 5% so v?i print()
- echo ' '.' ' nhanh hon 0.44% so v?i echo ' ',' '
6. H�m, phuong th?c v� c�ch th?c g?i
- call_user_func() ch?m hon 54% so v?i g?i tr?c ti?p h�m
- call_user_func() ch?m hon 59% so v?i g?i phuong th?c tinh (static method)
- call_user_func() ch?m hon 65% so v?i g?i phuong th?c (c?a m?t d?i tu?ng)
- function() nhanh hon 119% so v?i static::method()
- $this->method() nhanh hon 116% so v?i static::method()
- declared static::method() nhanh hon 93% so v?i static::method()
7. H�m, phuong th?c
- Truy?n tham tr? nhanh hon 3% so v?i truy?n tham chi?u
- Kh�ng tham chi?u (no reference ) nhanh hon 1.7% so v?i tham chi?u (Return by reference)
8. L�m vi?c v?i File
- Scandir() nhanh hon 4% so v?i opendir(), readdir(), closedir()
- file_get_contents() nhanh hon 52% so v?i fopen(), fread(), fclose()
- file_get_contents() nhanh hon 39% so v?i implode("n", file())
9. H�m ghi b? d?m (Cache)
- xcache_set() nhanh hon 1,645% so v?i file_put_contents()
- xcache_set() nhanh hon 646% so v?i memcache->set()
- xcache_get() nhanh hon 1,312% so v?i memcache->get()
10. H�m (T?ng qu�t)
- if elseif else nhanh hon 0.78 % so v?i switch
- @Error supression (b? b�o l?i n?u c�) ch?m hon 235% so v?i without
- $_SERVER['REQUEST_TIME'] nhanh hon 59% so v?i time()
- min(array) nhanh hon 16% so v?i min(int, int)
- require_once() nhanh hon 24% so v?i include()
- require_once() nhanh b?ng include_once()
- include('du?ng d?n tuong d?i') nhanh hon 37% so v?i include('du?ng d?n tuy?t d?i')
11. Chu?i ch�nh quy (regular expressions)
- str_replace() nhanh hon 40% so v?i preg_replace()
- ereg('regex') nhanh hon 17% so v?i preg_match('/regex/')
- preg_match('/regex/i') nhanh hon 68% so v?i eregi('regex')
12. Top 10 c�c h�m c� �ch nh?t - d?t gi� nh?t
file_put_contents()
opendir() readdir() closedir()
scandir()
fopen() fread() fclose()
memcache->get()
memcache->set()
implode('n', file())
include()
include_once()
file_get_contents()13. Top 10 c�c h�m ti?t ki?m th?i gian nhi?u nh?t ( biggest time savers (per call))
xcache_set() vs. file_put_contents()
xcache_get() vs. memcache->get()
xcache_set() vs. memcache->set()
file_get_contents() vs. fopen(), fread(), fclose()
file_get_contents() vs. implode('n', file())
include(relative) vs. include(full_path)
require_once() vs. include()
scandir() vs. opendir() readdir() closedir()
static method vs. call_user_func()
preg_match(/regex/i) vs. eregi(regex)