|
<?php |
|
|
|
require_once dirname(__FILE__) . '/HashSet.php'; |
|
require_once dirname(__FILE__) . '/TreeSet.php'; |
|
require_once dirname(__FILE__) . '/TreeSetX.php'; |
|
|
|
class BenchmarkTask |
|
{ |
|
function testStringSetWithInArray($N, $R) |
|
{ |
|
$keys = array(); |
|
for ($i = 0; $i < $N; $i++) { |
|
$key = 'KEY' . mt_rand(1, $R); |
|
if (!in_array($key, $keys)) { |
|
$keys[] = $key; |
|
} |
|
} |
|
} |
|
|
|
function testStringSetWithArrayUnique($N, $R) |
|
{ |
|
$seq = array(); |
|
for ($i = 0; $i < $N; $i++) { |
|
$key = 'KEY' . mt_rand(1, $R); |
|
$seq[] = $key; |
|
} |
|
$keys = array_unique($seq); |
|
} |
|
|
|
function testStringSetWithArrayKeys($N, $R) |
|
{ |
|
$map = array(); |
|
for ($i = 0; $i < $N; $i++) { |
|
$key = 'KEY' . mt_rand(1, $R); |
|
$map[$key] = null; |
|
} |
|
$keys = array_keys($map); |
|
} |
|
|
|
function _testIntSet($set, $N, $R) |
|
{ |
|
for ($i = 0; $i < $N; $i++) { |
|
$key = mt_rand(1, $R); |
|
$set->add($key); |
|
} |
|
} |
|
|
|
function _testStringSet($set, $N, $R) |
|
{ |
|
for ($i = 0; $i < $N; $i++) { |
|
$key = 'KEY' . mt_rand(1, $R); |
|
$set->add($key); |
|
} |
|
} |
|
|
|
function testIntHashSet($N, $R) |
|
{ |
|
$set = new HashSet(); |
|
self::_testIntSet($set, $N, $R); |
|
} |
|
|
|
function testStringHashSet($N, $R) |
|
{ |
|
$set = new HashSet(); |
|
self::_testStringSet($set, $N, $R); |
|
} |
|
|
|
function testIntTreeSet($N, $R) |
|
{ |
|
$set = new TreeSet(); |
|
self::_testIntSet($set, $N, $R); |
|
} |
|
|
|
function testStringTreeSet($N, $R) |
|
{ |
|
$set = new TreeSet(array($this, '_compareString')); |
|
self::_testStringSet($set, $N, $R); |
|
} |
|
|
|
function testIntTreeSetX($N, $R) |
|
{ |
|
$set = new TreeSetX(); |
|
self::_testIntSet($set, $N, $R); |
|
} |
|
|
|
function testStringTreeSetX($N, $R) |
|
{ |
|
$set = new TreeSetX(array($this, '_compareString')); |
|
self::_testStringSet($set, $N, $R); |
|
} |
|
|
|
function _compareString($a, $b) |
|
{ |
|
return strcmp($a, $b); |
|
} |
|
} |