Created
April 11, 2011 13:14
-
-
Save fukata/913496 to your computer and use it in GitHub Desktop.
対象データ数に比例しない突き合わせ処理サンプル
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 | |
function test1($masters, $targets) { | |
$sd = microtime(true); | |
$intersects = array_uintersect_assoc($targets, $masters, 'compare_key'); | |
$ed = microtime(true); | |
return $ed-$sd; | |
} | |
function compare_key($key1, $key2) { | |
if ($key2 == $key1->id) { | |
return 0; | |
} else if ($key2 > $key1->id) { | |
return 1; | |
} else { | |
return -1; | |
} | |
} | |
function test2($masters, $targets) { | |
$sd = microtime(true); | |
$intersects = array(); | |
foreach ($masters as $idx => $id) { | |
foreach ($targets as $u) { | |
if ($u->id == $id) { | |
$intersects[] = $u; | |
break; | |
} | |
} | |
} | |
$ed = microtime(true); | |
return $ed-$sd; | |
} | |
function generate_test_data($master_num, $target_num) { | |
$masters = array(); | |
for ($i=0; $i<$master_num; $i++) { | |
$masters[(string)$i] = mt_rand(); | |
} | |
$targets = array(); | |
for ($i=0; $i<$target_num; $i++) { | |
$targets[] = (Object)array('id'=>mt_rand()); | |
} | |
return array($masters, $targets); | |
} | |
$master_num = $argv[1]; | |
$target_num = $argv[2]; | |
$test_num = $argv[3]; | |
if ($master_num<=0 || $target_num<=0 || $test_num<=0) die('invalid arguments'); | |
$test1_results = array(); | |
$test2_results = array(); | |
for ($i=0; $i<$test_num; $i++) { | |
list($masters, $targets) = generate_test_data($master_num, $target_num); | |
$test1_results[] = test1($masters, $targets); | |
$test2_results[] = test2($masters, $targets); | |
} | |
for ($i=0; $i<$test_num; $i++) { | |
echo sprintf("%d: test1:%f, test2:%f\n", $i+1, $test1_results[$i], $test2_results[$i]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment