Skip to content

Instantly share code, notes, and snippets.

@v6ak
Created August 17, 2011 08:01
Show Gist options
  • Select an option

  • Save v6ak/1151069 to your computer and use it in GitHub Desktop.

Select an option

Save v6ak/1151069 to your computer and use it in GitHub Desktop.
ternary operator benchmark
#!/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)'
--- 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
--- 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
--- 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
@fprochazka
Copy link
Copy Markdown

Přidám ti svoje, chceš?

Výchozí balíček z distribuce:

$ ./benchmark-ternary.sh 
--- PHP version ---
PHP 5.3.5-1ubuntu7.2 with Suhosin-Patch (cli) (built: May  2 2011 23:00:17) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.1.1, Copyright (c) 2002-2011, by Derick Rethans
--- string: false benchmarks ---
== ternary ==
2.5061500072479

real    0m3.415s
user    0m2.860s
sys     0m0.530s

== pre-condition ==
3.2700290679932

real    0m4.180s
user    0m3.680s
sys     0m0.480s

== if-else ==
2.4046339988708

real    0m3.306s
user    0m2.730s
sys     0m0.560s

--- array: array-condition benchmarks ---
== ternary ==
2.5838911533356

real    0m3.106s
user    0m2.960s
sys     0m0.130s

== pre-condition ==
2.7533960342407

real    0m3.269s
user    0m3.120s
sys     0m0.130s

== if-else ==
2.3446300029755

real    0m2.852s
user    0m2.720s
sys     0m0.120s

--- array: count benchmarks ---
== ternary ==
4.1219100952148

real    0m4.628s
user    0m4.450s
sys     0m0.160s

== pre-condition ==
4.288526058197

real    0m4.794s
user    0m4.600s
sys     0m0.180s

== if-else ==
4.10680103302

real    0m4.608s
user    0m4.460s
sys     0m0.130s

--- array: isset benchmarks ---
== ternary ==
2.450502872467

real    0m2.956s
user    0m2.830s
sys     0m0.110s

== pre-condition ==
3.1764681339264

real    0m3.680s
user    0m3.510s
sys     0m0.150s

== if-else ==
2.420951128006

real    0m2.929s
user    0m2.830s
sys     0m0.090s

Druhý stroj, čerstvě podomácku kompilované php-fpm:

$ ./benchmark-ternary.sh 
--- PHP version ---
PHP 5.3.6 (cli) (built: Aug 15 2011 14:32:49) 
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
--- string: false benchmarks ---
== ternary ==
2.4762370586395

real    0m3.778s
user    0m2.940s
sys     0m0.770s

== pre-condition ==
2.7650101184845

real    0m4.346s
user    0m3.170s
sys     0m0.630s

== if-else ==
2.3474850654602

real    0m3.480s
user    0m2.870s
sys     0m0.590s

--- array: array-condition benchmarks ---
== ternary ==
2.5543909072876

real    0m3.103s
user    0m3.010s
sys     0m0.090s

== pre-condition ==
2.6322779655457

real    0m3.185s
user    0m3.070s
sys     0m0.120s

== if-else ==
2.5647759437561

real    0m3.081s
user    0m2.920s
sys     0m0.140s

--- array: count benchmarks ---
== ternary ==
4.5120301246643

real    0m5.045s
user    0m4.910s
sys     0m0.120s

== pre-condition ==
4.355320930481

real    0m4.856s
user    0m4.740s
sys     0m0.110s

== if-else ==
3.9904029369354

real    0m4.479s
user    0m4.380s
sys     0m0.100s

--- array: isset benchmarks ---
== ternary ==
2.5503849983215

real    0m3.105s
user    0m2.950s
sys     0m0.120s

== pre-condition ==
2.8092699050903

real    0m3.353s
user    0m3.240s
sys     0m0.100s

== if-else ==
2.4322481155396

real    0m2.950s
user    0m2.800s
sys     0m0.150s

@v6ak
Copy link
Copy Markdown
Author

v6ak commented Aug 22, 2011

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