Created
December 11, 2011 12:32
-
-
Save asaokamei/1460352 to your computer and use it in GitHub Desktop.
compare __, arry, and _ww performance.
This file contains 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 | |
/** | |
* compare performance of underscore.php, arry.php, and _ww.php. | |
*/ | |
require_once __DIR__ . '/../Underscore.php/underscore.php'; | |
require_once __DIR__ . '/../arry/Arry.php'; | |
require_once __DIR__ . '/../_ww/_ww.php'; | |
/** | |
* this data are copied from | |
* http://www.1x1.jp/blog/2011/12/php_arry.html | |
*/ | |
$data = array( | |
array('name' => 'Tigers', 'win' => 68, 'lose' => 70), | |
array('name' => 'Dragons', 'win' => 75, 'lose' => 59), | |
array('name' => 'Giants', 'win' => 71, 'lose' => 62), | |
array('name' => 'Swallows', 'win' => 70, 'lose' => 59), | |
array('name' => 'Carp', 'win' => 60, 'lose' => 76), | |
array('name' => 'Baystars', 'win' => 47, 'lose' => 86) | |
); | |
$try = 5000; | |
function pingtime() { | |
static $a=0; | |
if(! $a ) $a = microtime(TRUE); | |
$now = microtime(TRUE); | |
$ping = $now - $a; | |
$a = $now; | |
return $ping; | |
} | |
pingtime(); | |
// +--------------------------------------------------------------- + | |
// pure PHP. | |
for( $i = 0; $i < $try; $i++ ) { | |
measurePhp( $data ); | |
} | |
function measurePhp( $data ) { | |
foreach( $data as $k => $v ) { | |
$data[$k][ 'percent'] = $v['win'] / ($v['win'] + $v['lose'] ); | |
} | |
usort( $data, function($v1, $v2) { | |
return $v1['percent'] < $v2['percent'] ? 1 : -1; | |
}); | |
foreach( $data as $k => $v ) { | |
$data[$k]= sprintf("%-10s %02d %02d %.3f\n", $v['name'], $v['win'], $v['lose'], $v['percent']); | |
} | |
} | |
echo pingtime() . "\n"; | |
// +--------------------------------------------------------------- + | |
// measure arry. | |
for( $i = 0; $i < $try; $i++ ) { | |
$result = arr( $data ) | |
->map(function($v) { | |
$v['percent'] = $v['win'] / ($v['win'] + $v['lose']); | |
return $v; | |
})->usort(function($v1, $v2) { | |
return $v1['percent'] < $v2['percent'] ? 1 : -1; | |
})->map(function($v) { | |
return sprintf("%-10s %02d %02d %.3f\n", $v['name'], $v['win'], $v['lose'], $v['percent']); | |
})->y(); | |
} | |
echo pingtime() . "\n"; | |
// +--------------------------------------------------------------- + | |
// measure __ | |
for( $i = 0; $i < $try; $i++ ) { | |
$result = __( $data )->chain() | |
->map(function($v) { | |
$v['percent'] = $v['win'] / ($v['win'] + $v['lose']); | |
return $v; | |
})->sortBy(function($v1) { | |
return -$v1['percent']; | |
})->map(function($v) { | |
return sprintf("%-10s %02d %02d %.3f\n", $v['name'], $v['win'], $v['lose'], $v['percent']); | |
})->value(); | |
} | |
echo pingtime() . "\n"; | |
// +--------------------------------------------------------------- + | |
// measure _ww | |
for( $i = 0; $i < $try; $i++ ) { | |
$result = _ww::chain( $data ) | |
->map(function($v) { | |
$v['percent'] = $v['win'] / ($v['win'] + $v['lose']); | |
return $v; | |
})->sortBy(function($v1) { | |
return -$v1['percent']; | |
})->map(function($v) { | |
return sprintf("%-10s %02d %02d %.3f\n", $v['name'], $v['win'], $v['lose'], $v['percent']); | |
})->value(); | |
} | |
echo pingtime() . "\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment