Benchmarks by their nature are artificial:
We all know that so there is no need to prattle on about it. The results
of these tests indeed need to be taken with a grain or salt, or a shot of Bourbon. The point is however, that some things
are simply better practices and knowing those is a way forward and not an excuse to look back and change existing code.
Performance follows the classic 80/20 Rule, if test a vs test b does not have > 20% difference, you are very likely optimized.
How it works:
The total number of test loops is set to try run the longest test for ~1 second. This total is then sub divided in to 20 test runs to get an average and margin of error. The test order is random in each group to attempt to minimize any preference or impact of server load on one test group vs another. Completed tests that have a low margin of error are recorded in to the historical statistical record.
Benchmark Group: addslashes
- addslashes1: addslashes() vs. addcslashes()
- addslashes2: addslashes() vs. mysql_escape_string()
Benchmark Group: array
- array1: integer vs. string keys
- array2: arrays vs. variables
- array3: array_unique() vs. !isset() (integers)
- array4: array_push() vs. [] =
- array5: list () vs. [0]
- array6: set duplicates vs. !isset()
- array7: set unique vs. !isset()
- array8: array_diff() vs. ==
- array9: set immediately or later
- 1 hist array10: in_array() vs. key
- array11: array_keys(array_flip()) vs. array_unique()
Benchmark Group: array_map
- array_map1: array_map() vs. foreach
- 1 hist array_map2: array_map() vs. array_walk()
- array_map3: array_walk() vs. array_walk_recursive()
Benchmark Group: array_merge
- array_merge1: array_merge() vs. + (string keys)
- array_merge2: array_merge() vs. for (int keys)
Benchmark Group: cast
- 1 hist cast1: (int) vs. intval()
- cast2: (int) vs. settype()
- cast3: (bool) vs. !empty()
- cast4: (string) vs. interpolate
- cast5: (string) vs. concat
- 1 hist cast6: concat (string) vs. not
- cast7: (array) vs. array()
- cast8: (array) vs. array() w/is_array()
Benchmark Group: chr
- chr1: chr() v. literal
- chr2: chr() v. pack()
- chr3: ord() v. unpack() (str keys)
- 1 hist chr4: ord() v. unpack() (int keys)
- chr5: pack() v. sprintf()
Benchmark Group: classes
- classes1: single class vs. associative arrays
- classes2: many classes vs. associative arrays
- classes3: passing classes vs. arrays
- 1 hist classes4: function vs. class method
Benchmark Group: constants
- constants1: define constants vs. variables
- constants2: using constants vs. variables
- constants3: defined() vs. isset()
- constants4: redefine constant
Benchmark Group: create_function
- create_function1: create_function() vs. eval()
- create_function2: many create_function() vs. eval()
- create_function3: create_function() vs. func callback
- create_function4: create_function() vs. closure inside loop
- 1 hist create_function5: create_function() vs. closure outside loop
Benchmark Group: date
- date1: date() vs. strftime()
- 1 hist date2: time() vs. strtotime('now')
- date3: time() vs. microtime(true)
- 1 hist date4: strftime() vs. localtime()
- date5: strtotime('today') vs. mktime()
Benchmark Group: dir
- 1 hist dir1: glob() vs. glob no sort files only
- dir2: glob(no_sort) vs scandir() files only
- dir3: glob(no_sort) vs opendir/readdir files only
- dir4: glob(no_sort) vs DirectoryIterator()
- dir5: glob(no_sort) vs DirectoryIterator()
Benchmark Group: echo
- echo1: commas vs. concat on long strings
- echo2: commas vs. concat on short strings
- echo3: commas vs. interpolation on long strings
- echo4: commas vs. interpolation on short strings
- echo5: echo vs. ?>...<?php (static)
- echo6: echo vs. ?>...<?php (dynamic)
- echo7: commas vs. sprintf() on long strings
- echo8: commas vs. sprintf() on short strings
Benchmark Group: equal
- 1 hist equal1: == vs. === (fail, different types)
- equal2: == vs. === (different types with cast)
- equal3: == vs. === (fail, same types)
- equal4: == vs. === (pass, same types)
- equal5: === vs. strcmp() (fail)
- equal6: === vs. strcmp() (pass)
- equal7: === null vs. is_null()
- equal8: === null vs. == null
Benchmark Group: errors
- errors1: generating notices vs. not
- errors2: error_reporting(0) vs. @
- errors3: Exceptions vs. return code (fail)
- errors4: Exceptions vs. return code (pass)
- errors5: ignoring notices vs. not
- errors6: @ (no error)
Benchmark Group: exec
Benchmark Group: file
- 1 hist file1: file() vs. file_get_contents() (str)
- file2: file() vs. file_get_contents() (array)
- file3: file() vs. fgets() (array)
Benchmark Group: foreach
- foreach1: foreach reference reading
- foreach2: foreach reference writing
- foreach3: foreach $k => $dummy vs. array_keys()
- foreach4: foreach vs. while (list)
- foreach5: foreach/range() vs. for
- foreach6: foreach writing ref vs. key
- foreach7: foreach key vs. no key
- foreach8: foreach vs. while (next)
- foreach9: foreach (fcall) vs. foreach ($temp)
- 1 hist foreach10: foreach vs. for
Benchmark Group: func
- func1: fcall vs. not (non-trivial)
- func2: fcall vs. call_user_func()
- func3: fcall vs. call_user_func_array()
- 1 hist func4: fcall vs. callback
- func5: callback vs. if
- func6: callback vs. wrapped
- func7: callback vs. call_user_func()
- func8: callback vs. wrapped
Benchmark Group: global
Benchmark Group: hash
Benchmark Group: if
- if1: else if vs. elseif
- 1 hist if2: if vs. ternary
- if3: nested ifs
- 1 hist if4: if vs. switch
- if5: if vs. ternary large string
Benchmark Group: ifor
- ifor1: array_keys(array_flip()) vs. array_unique()
Benchmark Group: include
- include1: include vs. require
- 1 hist include2: include vs. eval()
- include3: include vs. include_once
- include4: file_exists() vs. require
- include5: include_once vs. function_exists()
Benchmark Group: index
- index1 (MySQL): performance penalty of one index
Benchmark Group: is
Benchmark Group: keycreation
- keycreation: strtr() vs. str_replace() on long strings
Benchmark Group: last
- last1: Last Character
Benchmark Group: math
- math1: float vs. integer addition
- math2: float vs. integer multiplication
- math3: multiply vs. add
- math4: min()/max() vs. if
- math5: abs() vs. ternary
Benchmark Group: ob_start
Benchmark Group: preg
- preg1: preg_replace() vs. str_replace()
- preg2: preg_replace() vs. stri_replace()
- preg3: preg_match() vs. strpos() (simple)
- preg4: preg_match() vs. strpos() (complex)
- 1 hist preg5: preg_split() vs. explode()/str_replace()
- 1 hist preg6: preg_replace() vs. preg_replace_callback()
Benchmark Group: rand
- rand1: rand() vs. mt_rand()
- rand2: rand() more vs. less
- rand3: rand() vs. array_rand() (odd keys)
- rand4: rand() vs. array_rand() (seq keys)
Benchmark Group: recurse
Benchmark Group: socket
Benchmark Group: strpios
Benchmark Group: strpos
- strpos1: strstr() vs. strpos()
- strpos2: strncmp() vs. strpos() (long)
- 2 hist strpos3: strncmp() vs. strpos() (short)
- 2 hist strpos4: substr() vs. strpos() (long)
- 2 hist strpos5: substr() vs. strpos() (short)
- strpos6: strpos()/strtolower() vs. stripos()
- 1 hist strpos7: substr_compare vs strpos
Benchmark Group: strtr
- strtr1: strtr() vs. str_replace() on long strings
- strtr2: strtr() vs. str_replace() on short strings
- strtr3: str_ireplace vs. strtolower + str_replace
- strtr4: strtr() vs. str_replace()/array_keys() on long strings
Benchmark Group: substr
- 1 hist substr1: substr($var, 5, 1) vs. {5}
- substr2: substr() negative vs. strlen()
- substr3: substr() on short vs. long strings
- 1 hist substr4: substr($var, 5, 1) vs. isset && {5}
- substr5: substr($var, 25, 1) vs. isset && {25}
Benchmark Group: urlencode
- urlencode1: urlencode() vs. rawurlencode()
- urlencode2: urlencode() vs. base64_encode()
- urlencode3: urlencode() vs. urldecode()