Skip to content

Instantly share code, notes, and snippets.

@hnw
Created May 21, 2017 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hnw/b17873796f8f18732cd0f2d80d8757cd to your computer and use it in GitHub Desktop.
Save hnw/b17873796f8f18732cd0f2d80d8757cd to your computer and use it in GitHub Desktop.
<?php
$array_size =isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : 1024;
$access_loop = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : 16;
$usleep_time = 200000; // 200ms
$loop_for_accuracy = ($array_size >= 1024) ? 1 : (1024 / $array_size);
// 事前準備
for ($j = 0; $j < $loop_for_accuracy; $j++) {
for ($i = 0; $i < $array_size; $i++) {
$keys[$j][] = "foo${j}_${i}";
}
}
for ($i = 0; $i < $access_loop; $i++) {
$j = rand(0, $array_size - 1);
$search_keys[] = $keys[0][$j];
}
usleep($usleep_time);
// 構築時間の測定(配列)
$start = microtime(true);
for ($i = 0; $i < $loop_for_accuracy; $i++) {
$tmp1 = array();
foreach ($keys[$i] as $key) {
$tmp1[] = $key;
}
$tmp2[$i] = $tmp1;
}
$time1 = (microtime(true) - $start) / $loop_for_accuracy;
var_dump($time1);
$a1 = $tmp2[0];
usleep($usleep_time);
// 構築時間の測定(連想配列)
$start = microtime(true);
for ($i = 0; $i < $loop_for_accuracy; $i++) {
$tmp3 = array();
foreach ($keys[$i] as $key) {
$tmp3[$key] = $key;
}
$tmp4[$i] = $tmp3;
}
$time2 = (microtime(true) - $start) / $loop_for_accuracy;
var_dump($time2);
$a2 = $tmp4[0];
usleep($usleep_time);
// アクセス時間の測定(配列)
$start = microtime(true);
foreach ($search_keys as $key) {
$ret = in_array($key, $a1, true);
}
$time3 = microtime(true) - $start;
var_dump($time3);
usleep($usleep_time);
// 構築時間の測定(連想配列)
$start = microtime(true);
foreach ($search_keys as $key) {
$ret = isset($a2[$key]);
}
$time4 = microtime(true) - $start;
var_dump($time4);
echo "\n";
printf("%.6f\n",$time1+$time3);
printf("%.6f\n",$time2+$time4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment