
Untitled
By: a guest on
May 10th, 2012 | syntax:
None | size: 1.10 KB | hits: 13 | expires: Never
#!/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)'