Created
August 17, 2011 08:01
-
-
Save v6ak/1151069 to your computer and use it in GitHub Desktop.
ternary operator benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| COUNT=1e6 | |
| FUNCTIONS=' | |
| function res($x){ | |
| //echo $x; | |
| }; | |
| ' | |
| #CREATE_ARRAY='$a = array_fill(0, '$COUNT', "fofo");' | |
| CREATE_ARRAY=' | |
| $a = array(); | |
| for($i=0; $i<'$COUNT'; $i++){ | |
| $a[$i] = $i; | |
| }; | |
| ' | |
| CREATE_STRING='$content = str_repeat("x", 1e9);' | |
| function run { | |
| echo "== $1 ==" | |
| loop=' | |
| $t1 = microtime(true); | |
| for($i=0; $i<10000000; $i++){ | |
| '$2' | |
| }; | |
| $t2 = microtime(true); | |
| echo $t2-$t1 . "\n"; | |
| ' | |
| time php --no-php-ini -d memory_limit=2G -r "$FUNCTIONS $3 $loop" | |
| echo | |
| } | |
| function benchmark { | |
| echo "--- $1 benchmarks ---" | |
| run "ternary" "res($2?1:2);" "$3" | |
| run "pre-condition" "\$cond = $2; res(\$cond?1:2);" "$3" | |
| #run "pre-condition-booleanize" "\$cond = (bool)($2); res(\$cond?1:2);" "$3" # na toto nemám nervy | |
| run "if-else" "if($2){res(1);}else{res(2);};" "$3" | |
| } | |
| function array_benchmark { | |
| benchmark "array: $1" "$2" "$CREATE_ARRAY" | |
| } | |
| function string_benchmark { | |
| benchmark "string: $1" "$2" "$CREATE_STRING" | |
| } | |
| echo "--- PHP version ---" | |
| php -v | |
| string_benchmark 'false' '$content!==false' | |
| array_benchmark array-condition '$a' | |
| array_benchmark count 'count($a)' | |
| array_benchmark isset 'isset($a)' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- PHP version --- | |
| PHP 5.3.6-pl0-gentoo (cli) (built: Jul 25 2011 17:13:38) | |
| Copyright (c) 1997-2011 The PHP Group | |
| Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies | |
| --- string: false benchmarks --- | |
| == ternary == | |
| 3.5682220458984 | |
| real 0m4.734s | |
| user 0m4.200s | |
| sys 0m0.520s | |
| == pre-condition == | |
| 3.8488600254059 | |
| real 0m5.007s | |
| user 0m4.420s | |
| sys 0m0.560s | |
| == if-else == | |
| 3.2100081443787 | |
| real 0m4.360s | |
| user 0m3.760s | |
| sys 0m0.590s | |
| --- array: array-condition benchmarks --- | |
| == ternary == | |
| 3.4548571109772 | |
| real 0m4.033s | |
| user 0m3.930s | |
| sys 0m0.100s | |
| == pre-condition == | |
| 4.3669090270996 | |
| real 0m4.933s | |
| user 0m4.820s | |
| sys 0m0.110s | |
| == if-else == | |
| 3.0520739555359 | |
| real 0m3.617s | |
| user 0m3.520s | |
| sys 0m0.090s | |
| --- array: count benchmarks --- | |
| == ternary == | |
| 5.4488241672516 | |
| real 0m6.013s | |
| user 0m5.900s | |
| sys 0m0.110s | |
| == pre-condition == | |
| 5.9039001464844 | |
| real 0m6.472s | |
| user 0m6.390s | |
| sys 0m0.070s | |
| == if-else == | |
| 5.4985609054565 | |
| real 0m6.064s | |
| user 0m5.920s | |
| sys 0m0.130s | |
| --- array: isset benchmarks --- | |
| == ternary == | |
| 3.7226839065552 | |
| real 0m4.296s | |
| user 0m4.160s | |
| sys 0m0.130s | |
| == pre-condition == | |
| 4.047434091568 | |
| real 0m4.614s | |
| user 0m4.530s | |
| sys 0m0.080s | |
| == if-else == | |
| 3.4137818813324 | |
| real 0m3.983s | |
| user 0m3.810s | |
| sys 0m0.140s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- PHP version --- | |
| PHP 5.3.6-pl0-gentoo (cli) (built: Jul 25 2011 17:13:38) | |
| Copyright (c) 1997-2011 The PHP Group | |
| Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies | |
| --- string: false benchmarks --- | |
| == ternary == | |
| 3.8175311088562 | |
| real 0m5.014s | |
| user 0m4.330s | |
| sys 0m0.620s | |
| == pre-condition == | |
| 3.7626440525055 | |
| real 0m4.902s | |
| user 0m4.310s | |
| sys 0m0.580s | |
| == if-else == | |
| 3.1675589084625 | |
| real 0m4.315s | |
| user 0m3.720s | |
| sys 0m0.590s | |
| --- array: array-condition benchmarks --- | |
| == ternary == | |
| 3.425420999527 | |
| real 0m3.998s | |
| user 0m3.880s | |
| sys 0m0.110s | |
| == pre-condition == | |
| 3.6822090148926 | |
| real 0m4.246s | |
| user 0m4.130s | |
| sys 0m0.110s | |
| == if-else == | |
| 3.0865190029144 | |
| real 0m3.651s | |
| user 0m3.560s | |
| sys 0m0.080s | |
| --- array: count benchmarks --- | |
| == ternary == | |
| 5.5451538562775 | |
| real 0m6.113s | |
| user 0m6.010s | |
| sys 0m0.100s | |
| == pre-condition == | |
| 5.9618690013885 | |
| real 0m6.533s | |
| user 0m6.410s | |
| sys 0m0.120s | |
| == if-else == | |
| 5.6266150474548 | |
| real 0m6.211s | |
| user 0m6.080s | |
| sys 0m0.120s | |
| --- array: isset benchmarks --- | |
| == ternary == | |
| 3.7437579631805 | |
| real 0m4.316s | |
| user 0m4.170s | |
| sys 0m0.140s | |
| == pre-condition == | |
| 4.1273038387299 | |
| real 0m4.757s | |
| user 0m4.600s | |
| sys 0m0.110s | |
| == if-else == | |
| 3.4138669967651 | |
| real 0m3.979s | |
| user 0m3.870s | |
| sys 0m0.100s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- PHP version --- | |
| PHP 5.3.6-pl0-gentoo (cli) (built: Jul 25 2011 17:13:38) | |
| Copyright (c) 1997-2011 The PHP Group | |
| Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies | |
| --- string: false benchmarks --- | |
| == ternary == | |
| 3.4846639633179 | |
| real 0m4.653s | |
| user 0m3.970s | |
| sys 0m0.650s | |
| == pre-condition == | |
| 3.7664549350739 | |
| real 0m4.911s | |
| user 0m4.360s | |
| sys 0m0.530s | |
| == if-else == | |
| 3.3687100410461 | |
| real 0m4.512s | |
| user 0m3.980s | |
| sys 0m0.530s | |
| --- array: array-condition benchmarks --- | |
| == ternary == | |
| 3.928768157959 | |
| real 0m4.529s | |
| user 0m4.450s | |
| sys 0m0.070s | |
| == pre-condition == | |
| 3.6652979850769 | |
| real 0m4.227s | |
| user 0m4.120s | |
| sys 0m0.100s | |
| == if-else == | |
| 3.0571429729462 | |
| real 0m3.619s | |
| user 0m3.500s | |
| sys 0m0.110s | |
| --- array: count benchmarks --- | |
| == ternary == | |
| 5.3405048847198 | |
| real 0m5.906s | |
| user 0m5.850s | |
| sys 0m0.050s | |
| == pre-condition == | |
| 5.9796879291534 | |
| real 0m6.556s | |
| user 0m6.400s | |
| sys 0m0.150s | |
| == if-else == | |
| 5.3810119628906 | |
| real 0m5.952s | |
| user 0m5.790s | |
| sys 0m0.160s | |
| --- array: isset benchmarks --- | |
| == ternary == | |
| 3.7650849819183 | |
| real 0m4.335s | |
| user 0m4.240s | |
| sys 0m0.090s | |
| == pre-condition == | |
| 4.0186460018158 | |
| real 0m4.597s | |
| user 0m4.520s | |
| sys 0m0.070s | |
| == if-else == | |
| 3.464791059494 | |
| real 0m4.027s | |
| user 0m3.890s | |
| sys 0m0.120s |
Author
Díky, o to mi ale nešlo. Pokoušel jsem se změřit výkonnostní rozdíl mezi ternárem a ifem, ale benchmark jsem napsal blbě. "Velké hodnoty" jsou jen v podmínce, ne ve větvích. Doma jsem to již opravil a rozdíl je znát. (A počet iterací v benchmarku jsem snížil na deset, abych se dočkal...)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Přidám ti svoje, chceš?
Výchozí balíček z distribuce:
Druhý stroj, čerstvě podomácku kompilované php-fpm: